restrict collection.find() should only return non-empty object but not empty objects inside array - sql

const response = await simpleSurveyResponsesModel
.find({
"metaKey.projectId": 7,
})
.select([
"answers.SEC.text",
"answers.S4.text",
"answers.S3_a.text",
"answers.Urban_City.text"
])
.lean();
Where answers feild is array of objects containing both empty ad non-empty objects.
collection.find() returns the selected objects but also empty objects as well, inside answers array which i dont need.
My Output
{
"_id": "61840979fcbea215bc61ed03",
"answers": [
{},
{},
{
"Urban_City": {
"text": "Hyderabad"
}
},
]
}
Expected Output
{
"_id": "61840979fcbea215bc61ed03",
"answers": [
{
"Urban_City": {
"text": "Hyderabad"
}
},
]
}

Related

Can't make PUT /raylight/v1/documents/id/parameter/id work properly

I need to update document parameters via REST API.
I've tried using the following:
PUT .../raylight/v1/documents/33903/parameters/3
with the following json payload
{
"parameters":{
"parameter": {
"id": 3,
"answer": {
"values": {
"value": [
"2019/9"
]
}
}
}
}
}
But the returned answer shows unmodified parameters:
{
"parameter": {
"#optional": "false",
"#type": "prompt",
...
"id": 3,
...
"answer": {
...
"info": {
...
"previous": {
"value": [
"2015\/12"
]
}
},
"values": {
"value": [
"2015\/12"
]
}
}
}
}
How can I properly set new prompt parameters?
Do:
PUT .../raylight/v1/documents/33903/parameters
instead of:
PUT .../raylight/v1/documents/33903/parameters/3
Adding a parameter ID at the end performs a different function: it returns the list of parameters that are dependent upon the one provided. You have only one in this case, and it's returning itself. Leave it off, to refresh the document.

knex json array of object where condition

I have a json array (composition) in my db and i need to make conditions
[
{
"name": "Rock",
"prices": [
{
"price": 400,
"duration": 40
}
],
"description": "Hello",
"nb_musicians": 40
}
]
And i try something like this but dont work
.where(qb => {
if (nbMusicians) {
qb.whereRaw(
`composition->
'$.nb_musicians' = ?`,
[40],
);
}
})
Thanks for help

Karate: Match JSON Array responses where the order of array is different in each hit

I have a scenario where a portion of the response arrays is the response from a child API.
child API response looks like below, but there is no specific order. And I need to check whether the child API response is present in the parent API(irrespective of the order of the elements in the child API). I followed this Karate - Match two dynamic responses thread but its not working in my case.
* def response1 =
"""
{
"array1": [
{
"element": {
"id": "A1",
"array11": [
{
"uid": "u123",
"gid": [
"g1"
]
}
]
}
},
{
"element": {
"id": "A2",
"array11": [
{
"uid": "u124",
"gid": [
"g2"
]
}
]
}
}
]
}
"""
* def response2 =
"""
{
"array1": [
{
"element": {
"id": "A2",
"array11": [
{
"uid": "u124",
"gid": [
"g2"
]
}
]
}
},
{
"element": {
"id": "A1",
"array11": [
{
"uid": "u123",
"gid": [
"g1"
]
}
]
}
}
]
}
"""
This is a one liner :)
* match response2.array1 contains response1.array1
Guess what, you don't have to match pure JSON all the time, using child-sections is fine.
But also read this specific part of the docs: https://github.com/intuit/karate#contains-short-cuts
And this example: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/graphql/graphql.feature

Lodash: filter multiple properties

I am new to lodash.
I am having trouble filtering with lodash. I have a deep nested json object that I want to filter if productName = 'Lotto' and the board selectionMethod = "AUTOPICK"
When I try the solution below, it returns all results instead of filtering. I have tried multiple ways to do this but I always get all results returning.
Could anyone offer a suggestion?
var results = {
"buyTicketDetails": {
"result": 0,
"message": "Success",
"product": [
{
"productName": "Lotto",
"displayPromoMessage": false,
"drawDetails": [
{
"drawTypeDescription": "REGULAR DRAW",
"drawAttribute": "EVENING",
"drawStartDate": "2019-01-12T00:00:00.000-05",
"drawEndDate": "2019-01-12T00:00:00.000-05"
},
{
"drawTypeDescription": "SPECIAL DRAW",
"drawAttribute": "EVENING",
"drawStartDate": "2019-01-12T00:00:00.000-05",
"drawEndDate": "2019-01-12T00:00:00.000-05"
}
],
"board": [
{
"boardType": "REGULAR",
"selectionMethod": "AUTOPICK",
"selectionSet": [
"2",
"4",
"10",
"12",
"17",
"31"
]
},
{
"boardType": "RAFFLE",
"selectionMethod": "SYSTEMPICK",
"selectionSet": [
"40001722-01"
]
}
]
},
{
"productName": "Encore",
"displayPromoMessage": false,
"drawDetails": [
{
"drawTypeDescription": "REGULAR DRAW",
"drawAttribute": "EVENING",
"drawStartDate": "2019-01-12T00:00:00.000-05",
"drawEndDate": "2019-01-12T00:00:00.000-05"
}
],
"board": [
{
"boardType": "REGULAR",
"selectionMethod": "SYSTEMPICK",
"selectionSet": [
"3440514"
]
}
]
}
]
}
}
const filterCat = _.filter(results, { product: [
{
productName: "Lotto",
board: {
selectionMethod: "AUTOPICK"
}}
]
}
);
console.log(filterCat);
With Pure JS.
You can do this with Javascript's filter function too.
filter function actually works on Arrays, so we use map loop to add objects into arrays first, then we use filter function to get only the data we need!.
let Product = results.buyTicketDetails.product
let getSelectionMethods=(index) => Product[index].board.map((d,i)=>d.selectionMethod)
let getTargetedProducts =(Name,Method)=> Product.map((d,i)=>{
if(Product[i].productName==Name && getselectionMethods(i).indexOf(Method) !==-1){
return d
}
})
let FilteredProducts = getTargetedProducts("Lotto","AUTOPICK").filter((d)=>d !==undefined)
console.log(FilteredProducts)

WebAPI 2.2 OData v3 Jagged Array empty result

I'm trying to implement a simple WebAPI that returns Data from a class. All data is received correctly exept the jagged string array.
This is the code:
public IHttpActionResult GetGetGeoData_Result()
{
var results = db.GetGeoData().ToList();
List<GeoJsonResult> jsonResult = new List<GeoJsonResult>();
results.ForEach(r => jsonResult.Add(new GeoJsonResult()
{
Type = "Feature",
Geometry = new GeoJsonGeometry()
{
Type = r.geo_type,
Coordinates = this.CalculateCoordinates(r.coordinates) // This result is empty => Jagged string array (string[][])
},
Properties = new GeoJsonProperties()
{
Name = "SomeName"
}
}));
return Ok(jsonResult);
}
This is the output:
{
"odata.metadata": "http://localhost:65372/odata/$metadata#GetGeoData_Result",
"value": [
{
"Type": "Feature",
"Geometry": {
"Type": "Polygon",
"Coordinates": [
{},
{},
{},
{},
{},
{}
]
},
"Properties": {
"Name": "SomeName"
}
}]
}
What am I doing wrong?