Is there a mongoose function in order to find a field value and the amount of times it exists in the document.
For example:
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "411111"
},
"Info": {
"fullName": "full name",
"address": "address",
"city": "city",
"pcode": "PostCode",
"dob": "01/01/2022",
"bin": "400000"
},
I need the function to return something like:
bin: 411111, count: 3
bin: 400000, count: 1
You can use the following aggregation pipeline to achieve exactly what you want:
.aggregate([
{
$group: {
_id: "$Info.bin",
count: {
$sum: 1
}
}
}
])
Related
I have Avro Schema: customer record import the CustomerAddress subset.
[
{
"type": "record",
"namespace": "com.example",
"name": "CustomerAddress",
"fields": [
{ "name": "address", "type": "string" },
{ "name": "city", "type": "string" },
{ "name": "postcode", "type": ["string", "int"] },
{ "name": "type","type": {"type": "enum","name": "type","symbols": ["POBOX","RESIDENTIAL","ENTERPRISE"]}}
]
},
{
"type": "record",
"namespace": "com.example",
"name": "Customer",
"fields": [
{ "name": "first_name", "type": "string" },
{ "name": "middle_name", "type": ["null", "string"], "default": null },
{ "name": "last_name", "type": "string" },
{ "name": "age", "type": "int" },
{ "name": "height", "type": "float" },
{ "name": "weight", "type": "float" },
{ "name": "automated_email", "type": "boolean", "default": true },
{ "name": "customer_emails", "type": {"type": "array","items": "string"},"default": []},
{ "name": "customer_address", "type": "com.example.CustomerAddress" }
]
}
]
i have JSON payload:
{
"Customer" : {
"first_name": "John",
"middle_name": null,
"last_name": "Smith",
"age": 25,
"height": 177.6,
"weight": 120.6,
"automated_email": true,
"customer_emails": ["ning.chang#td.com", "test#td.com"],
"customer_address":
{
"address": "21 2nd Street",
"city": "New York",
"postcode": "10021",
"type": "RESIDENTIAL"
}
}
}
when i runt the command: java -jar avro-tools-1.8.2.jar fromjson --schema-file customer.avsc customer.json
got the following exception:
Exception in thread "main" org.apache.avro.AvroTypeException: Unknown union branch Customer
In your JSON data you use the key Customer but you have to use the fully qualified name. So it should be com.example.Customer.
I am starting out with Postman. I made a simple GET request and got a response. At the end of the response, is "count=14". I am trying to understand what count means here?
I thought the number of records in the response is 14, but its only 10. So what does count=14 mean? Thankyou for your response!
This is the response I got:
{
"records": [
{
"id": "074df210-e1fb-4298-9407-d97881dc1af2",
"fullName": "Abcd",
"email": "janedoe#gmail.com",
"phone": "112-112-112"
},
{
"id": "4f0835d7-7c02-43da-86c7-77a03b4",
"fullName": "John Doe",
"email": "johndoe#gmail.com",
"phone": "1234-234"
},
{
"id": "a9e8cfb6-f62a-4a8e-84a5-df30",
"fullName": "Kitty Doe",
"email": "kittydoe#gmail.com",
"phone": "123-493-892"
},
{
"id": "319bdb34-e1c1-4e86-192",
"fullName": "Lovely Doe",
"email": "lovelydoe#gmail.com",
"phone": "089-829-3898"
},
{
"id": "1dfe5bba-e14b-4e9d-b90c",
"fullName": "Memory Doe",
"email": "memorydoe#gmail.com",
"phone": "273-872-8398"
},
{
"id": "61f466d5-ec00-76ccb88fff5e",
"fullName": "asdas",
"email": "asdas#gmail.com",
"phone": "123-123-9231"
},
{
"id": "f77b8e1d-70f2-4d44-b243-e61eac2a41a0",
"fullName": "asdasda",
"email": "asdasda#gmail.com",
"phone": "123-123-1291"
},
{
"id": "7d071c9e-fce6-4732-bed3-c18412a962c1",
"fullName": "Monday",
"email": "monday#gmail.com",
"phone": "123-129-1231"
},
{
"id": "5a1d1e9d-7396-41e3-aa96-864b",
"fullName": "Tuesday",
"email": "tuesday#gmail.com",
"phone": "123-241-2534"
},
{
"id": "f5da56d1-9fc2861e6da3",
"fullName": "Wednesday",
"email": "wednesday#gmail.com",
"phone": "129-121-1212"
}
],
"count": 14
}
How to use scenario outline to iterate for a JSON which is more than single cell
https://github.com/intuit/karate#the-cucumber-way
Data.json
[{
"address": {
"addressLine1": "ttes",
"addressLine2": "Test",
"addressLine3": "Test",
"addressType": "business",
"city": "TEST",
"company": "TEST",
"country": "TEST",
"state": "TEST",
"postalCode": "XXXX"
},
"name": {
"firstName": "TEST",
"lastName": "TEST",
"middleInit": "T",
"title": "Mr."
},
"phone": {
"phoneExtension": "1234",
"phoneNumber": "999999999",
"phoneType": "mobile"
},
"email": {
"emailAddress": "TEST#TEST.com"
}
},
{
"address": {
"addressLine1": "ttes1",
"addressLine2": "Test1",
"addressLine3": "Test1",
"addressType": "business",
"city": "TEST1",
"company": "TEST1",
"country": "TEST1",
"state": "TEST1",
"postalCode": "XXXX"
},
"name": {
"firstName": "TEST1",
"lastName": "TEST1",
"middleInit": "T",
"title": "Mr."
},
"phone": {
"phoneExtension": "1234",
"phoneNumber": "999999999",
"phoneType": "mobile"
},
"email": {
"emailAddress": "TEST#TEST1.com"
}
}]
====
Feature: Read from File
Background:
* def Testdata = read('Data.json')
Scenario Outline: [1] Test with multiple records on JSON
Given url_stg
And path 'test','test'
And request Testdata
When method POST
Examples:
|Testdata |
Please refer it in this documentation as it very straight forward for your case.
dynamic-scenario-outline
refer to this answer for different ways of providing data to Scenario Outline:
https://stackoverflow.com/a/56135752/8615449
Is it possible to give select a schema to match the case on?
The use case I have is that I want to select and match on a list of accepted values:
{
"type": "object",
"properties": {
"addressCountry": {
"type": ["string", "null"]
}
},
"select": {
"$data": "0/addressCountry"
},
"selectIf": {
"type": "string",
"enum": ["FR", "JP", "US", "NZ", "DE"]
},
"selectThen": {
"addressCountry": {
"type": "string"
}
}
}
EDIT: please note that selectIf and selectThen aren't keywords supported by AJV. It does support selectCases and selectDefault, however with selectCases you have to state each case individually. My question is more about if it's possible to match on multiple cases and use a single definition.
#Relequestual managed to help me solve this outside of SO. I didn't need to use select at all, could just use if/then/else like so:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"contactByPost": {
"type": "boolean"
},
"streetAddress1": {
"type": ["string", "null"]
},
"streetAddress2": {
"type": ["string", "null"]
},
"streetAddress3": {
"type": ["string", "null"]
},
"locality": {
"type": ["string", "null"]
},
"region": {
"type": ["string", "null"]
},
"postCode": {
"type": ["string", "number", "null"]
},
"country": {
"type": ["string", "null"]
}
},
"if": {
"type": "object",
"properties": {
"contactByPost": {
"type": "boolean",
"const": true
}
}
},
"then": {
"if": {
"type": "object",
"properties": {
"country": {
"type": "string",
"enum": ["JP", "US"]
}
}
},
"then": {
"type": "object",
"properties": {
"contactByPost": { "type": "boolean" },
"streetAddress1": { "type": "string" },
"locality": { "type": "string" },
"region": { "type": "string" },
"postCode": { "type": ["string", "number"] },
"country": { "type": "string" }
},
"required": ["streetAddress1", "locality", "region", "postCode", "country"]
},
"else": {
"type": "object",
"properties": {
"contactByPost": { "type": "boolean" },
"streetAddress1": { "type": "string" },
"locality": { "type": "string" },
"region": { "type": ["string", "null"] },
"country": { "type": "string" }
},
"required": ["streetAddress1", "locality", "country"]
}
}
}
Would provide the following results:
// Valid
{
"contactByPost": false
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": "State",
"postCode": "12345",
"country": "JP"
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": "State",
"postCode": "12345",
"country": "US"
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": "State",
"postCode": "12345",
"country": "NZ"
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": null,
"postCode": null,
"country": "NZ"
}
// Invalid
{
"contactByPost": true
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": null,
"postCode": null,
"country": "JP"
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": null,
"postCode": null,
"country": "US"
}
I am using soapui with groovy for automation api test and I post a request REST, here is my response:
{`enter code here`
"firstname": "aaa",
"lastname": "bbb",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "326"
}, {
"firstname": "ddd",
"lastname": "eee",
"address": "test",
"city": "city",
"country": "country",
"default": "True",
"id": "67"
}, {
"firstname": "hhh",
"lastname": "yyy",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "345"
}, {
"firstname": "ooo",
"lastname": "hh",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "3211"
},
I want to recover the id of the user who has a default "true"
Firstly, the response you posted is not valid json.
Should be something like this:
[
{
"firstname": "aaa",
"lastname": "bbb",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "326"
},
{
"firstname": "ddd",
"lastname": "eee",
"address": "test",
"city": "city",
"country": "country",
"default": "True",
"id": "67"
},
{
"firstname": "hhh",
"lastname": "yyy",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "345"
},
{
"firstname": "ooo",
"lastname": "hh",
"address": "test",
"city": "city",
"country": "country",
"default": "false",
"id": "3211"
}
]
If you're just interested in the groovy script, here it is:
package json
import com.eviware.soapui.support.XmlHolder
import groovy.json.*
//define the location of the JSON response
def response = context.expand('${post_json_data#Response}').toString()
//parse response as json
def json_response = new JsonSlurper().parseText(response)
//iterate on the list with if condition
json_response.eachWithIndex {
item, index ->
if(item.default == 'True') {
log.info "index: ${index}"
log.info "Item default value: ${item.default}"
log.info "Item id value : ${item.id}"
}
}