I'm having dataset in which json consists of array having multiple json in it.
The objects in the array have 4 keys, out of which 1 is missing in some of them.
I want to get the documents where all the objects in an array are missing the key.
eg, From these following documents:
{"test":1,desc:[{"price":1,"abc":"def"},{"price":2,"ac":"def"}]}
{"test":1,desc:[{"price":1,"abc":"def"},{"ac":"def"}]}
{"test":1,desc:[{"abc":"def"},{"ac":"def"}]}
I want to match only the last document.
Thanks in advance..
You can just use $exists :
db.collection.find(
{
"desc.price": {$exists: false}
}
)
And if you want for other fields as well:
db.collection.find(
{
$or: [
{
"desc.price": {$exists: false}
},
{
"desc.abc": {$exists: false}
},
{
"desc.ac": {$exists: false}
}
]
}
)
One thing to note is that an empty array, ie desc = []
will always be matched by this query. if you want to ensure there's atleast one object use this query:
db.collection.find(
{
$and: [
{
"desc.price": {$exists: false}
},
{
"desc.0": {$exists: true}
}
]
}
)
Achieved that, just made a script where i used an array to store the price thing and then used.every method for price to be undefined, it returned me the desired result which i want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.