I have a collection of documents that look like this:
{
my_array : [
{
name : "...",
flag : 1 // optional
},
...
]
}
How can I query for documents that exactly one of their elements contain the flag field?
This will return documents where my_array length is exactly 1:
db.col.find({ "my_array" : { $size : 1 } })
This will return documents where at least one of my_array's object contain the flag field:
db.col.find({ "my_array.flag" : { $exists : true } })
And this will look for documents where flag is an array of size one:
db.col.find({ "my_array.flag" : { $size : 1 } })
I want to somehow combine the first two queries, checking for some inner field existence, and then querying for the size of the filtered array
flagwith some value (which is not important)