{
"sections": [
{
"desc": "no flow ID",
"sectionObj": [
{
"smartFlowIdList": []
}
]
},
{
"desc": "has flow ID",
"sectionObj": [
{
"smartFlowIdList": [
"smartFlowId1",
"smartFlowId2"
]
}
]
]
}
// Query document to see if any of the sections[].sectionObj[].smartFlowIdList has at least a flowId1
// in this case: it is True since [ "smartFlowId1", "smartFlowId2" ] exits
I need to query this document with MongoDB shell commands.
I have tried looking at $[], $all and digging up on mongo documentation but can seem to get anywhere. Any idea is appreciated, thanks!
One option is to iterate twice, per each nested level and count the items in last nested level. $match documents with count greater than 0:
db.collection.aggregate([
{$match: {$expr:
{$gt: [
{$sum: {$map: {
input: "$sections",
as: "external",
in: {$sum: [
{$reduce: {
input: "$$external.sectionObj",
initialValue: 0,
in: {$sum: ["$$value", {$size: "$$this.smartFlowIdList"}]}
}
}
]
}
}}},
0
]}
}}
])
See how it works on the playground example
Related
am using opensearch 2.4 and I have an index with some fields while creating , later i started saving new field to the index , now when i query on the newly created field am not getting any results
ex : query 1
POST abc/_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"name": [
"john"
]
}
}
]
}
}
}
above works fine because name fields exists since creation of index
query 2 :
POST abc/_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"lastname": [
"William"
]
}
}
]
}
}
}
above query doesnt work though i have some documents with lastname william
When you index a new field without previously declaring it in the mapping, opensearch/elastic will generate text type and type keyword.
There are two ways for you to get results with the Term Query. First remember that Term query works with exact terms.
The first option is to use the keyword field.
{
"terms": {
"lastname.keyword": [
"William"
]
}
}
The second option is to search in the text field, but remember that when indexing the default parser is applied, then the lowecase filter leaves the token like this: william.
In this case, the query should be:
{
"terms": {
"lastname": [
"william"
]
}
}
When you use "terms" there must be an exact match (including casing).
So make sure your document contains William and not william or Williams
If you want more tolerance you can explore the match query:
https://opensearch.org/docs/latest/opensearch/query-dsl/full-text/#match
Demo https://mongoplayground.net/p/_RyWWepvpyD.
I found examples to do with spring-mongodb. But unable to find any working way to do this with mongodb driver based code.
Query:
db.collection.update({
pollID: 123
},
{
"$inc": {
"answerAnalytics.$[element].selectCount": 1
}
},
{
"arrayFilters": [
{
"$or": [
{
"element.option": "1"
},
{
"element.option": "2"
}
]
}
],
"multi": true
})
DBCollectionFindAndModifyOptions dbCollectionFindAndModifyOptions = (new DBCollectionFindAndModifyOptions()).returnNew(true).upsert(false).update(incrObj).arrayFilters(arrayFilters);
dbCollection.findAndModify(queryDocument, dbCollectionFindAndModifyOptions);
I am looking for a Mongo query for finding data in the following document.
{
"key1": [{
"subkey1": ["america.south.gas"],
"subkey2": ["9898989898"]
}],
"key2": [{
"subkey1": ["america"],
"subkey2": ["hsadjsahjsahdjsah879878u9"]
},
{
"subkey1": ["america.south.gas","america"],
"subkey2": ["hsadjsahjsahdjsah879878u9"]
},
{
"subkey1": ["america.south.#"],
"subkey2": ["sjadkjsahdkjsahdj989s89d8sa98d9sa"]
}]
}
I want only subkey2 only of above mentioned document as following output:
"subkey2": ["hsadjsahjsahdjsah879878u9"]
"subkey2": ["sjadkjsahdkjsahdj989s89d8sa98d9sa"]
Now I want to fetch data with following query:-
db.collectionName.find({$or:[
{"key2.subkey1": "america.south.gas"},
{"key1.subkey1": "america.south.#"}
]},
{"_id": 0, "key2.subkey2.$": 1}
);
But it is showing me this error:
{
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "Executor error during find command: BadValue: positional operator (key1.$) requires corresponding field in query specifier",
"code" : 96
}
Any idea how can I achieve this for getting specific field with multiple query field in Mongo find operation??
This error ...
Error: error: { "waitedMS" : NumberLong(0), "ok" : 0, "errmsg" : "Executor error during find command: BadValue: positional operator (key1.$) requires corresponding field in query specifier", "code" : 96 }
.. is caused by this projection:
"key2.subkey2.$": 1
Soecifically, the positional operator: $.
According to the comments above, you ...
want only subkey2 only of above mentioned document as following output:- "subkey2": ["hsadjsahjsahdjsah879878u9"] "subkey2": ["sjadkjsahdkjsahdj989s89d8sa98d9sa"]
The following command ...
db.collection.aggregate([
{$unwind:'$key2'},
// find the documents having key2.subkey1 = 'america.south.#' or key2.subkey1 = 'america.south.gas'
{$match:{'key2.subkey1':{$in:['america.south.#','america.south.gas']}}},
// limit the output to only those key2.subkey2 entries for which the associated key2.subkey1 match the previous $match clause
{$group:{_id:0,'subkey2':{$push:'$key2.subkey2'}}},
// only return subkey2
{$project: {_id: 0, 'subkey2':1}}
])
... will return:
{
"subkey2" : [
[
"hsadjsahjsahdjsah879878u9"
],
[
"sjadkjsahdkjsahdj989s89d8sa98d9sa"
]
]
}
#glytching, If i need output like that as below given then what need to change in query and also no duplicate output value.
{
"subkey2" : [ "hsadjsahjsahdjsah879878u9","sjadkjsahdkjsahdj989s89d8sa98d9sa"]
}
I'm writing a smoke test using rest-assured and want to traverse the api to make sure no unexpected errors occur.
I have a data structure that looks like this:
{
...
"sites": [
{
...
"groups": [
{
...
"locations": [
{
...
"racks": [
{
"rackId": 123456789,
...
},
{
"rackId": 987654321,
...
},
...
]
}
]
}
]
},
{
...
"groups": [
{
...
"locations": [
{
...
"racks": [
{
"rackId": 11111111,
...
},
{
"rackId": 22222222,
...
},
...
]
}
]
}
]
},
...
]
}
Using JsonPath bundled in RestAssured I'm trying to get a flat list of all rackIds to then call subsequent requests for these rackIds.
jsonPath.getList("sites.groups.locations.racks.rackId", Long.class);
>> java.lang.NumberFormatException: For input string: "[[[406071537, 406071538, 406071539, 406071540, 406071541]]]"
I tried using this path, but didn't work because I believe this syntax only works with the other JsonPath implementation, not the one bundled with rest-assured
"$.sites[*].groups[*].locations[*].racks[*].rackId"
I now came down to this, that gives me lists of lists of lists, that I could then flatten myself. But I then have the issue, that the numbers interpreted as Integers by default, but I'm receiving Long values.
List list = jsonPath.getList("sites.groups.locations.racks.rackId");
Any ideas?
Just use flatten():
List list = jsonPath.getList("sites.groups.locations.racks.rackId.flatten()");
This is my dbcollection in mongodb
{
"_id" : 0,
"name" : "+Anima",
"author" : "MUKAI Natsumi",
"type" : [
"Adventure",
"Fantasy"
],
"chapters" : [
{
"chapterName" : "+Anima 56",
"chapterLink" : "http://...",
"groupTranslate" : "Manga Palace Group",
"dateUpload" : 1359478800000,
"chapterNumber" : 56.0000000000000000,
"images" : [
"img0",
"img1",
"img2",
"img3"
]
},
{
"chapterName" : "+Anima 55",
"chapterLink" : "http://....",
"groupTranslate" : "Manga Palace Group",
"dateUpload" : 1410109200000,
"chapterNumber" : 55.0000000000000000,
"images" : [
"img0",
"img1",
"img2",
"img3"
]
}
]
}
I cannot find list chapter sort by "dateUpload" in mongodb using java code. Please help.
Mongo $aggregation will solve your problem. Below query first $unwind your chapters array and then sort dateUpload
db.collectionName.aggregate({
"$unwind": "$chapters" // unwind breaks the chapters array
}, {
"$sort": {
"chapters.dateUpload": -1 // then seperated chapters array sorted by dateUpload
}
}, {
"$group": {
"_id": "$_id",
"chapters": {
"$push": "$chapters" // group used for grouping chapters array
}
}
}).pretty()
And for converting above query in java code formatt then follow below mongo aggregation java driver code .
Java Driver and Aggregation Framework
This should do it, using aggregate():
db.foo.aggregate( [ { $project: {chapters: 1} },
{ $unwind: "$chapters" },
{ $sort: {"chapters.dateUpload": -1} }
] );
Extract the chapters subdocument with $project, then $unwind it to deconstruct the array and get each element, finally $sort them on dateUpload value (descending, use 1 for ascending order).