In the answer to a question I found a interesting solution for searching array values using $elemMatch.
If we have the following documents in our collection:
{
foo : [ { bar : "xy", baz : 1 },
{ bar : "a", baz : 10 } ]
},
{
foo : [ { bar : "xy", baz : 5 },
{ bar : "b", baz : 50 } ]
}
The following query will match only the first document:
db.test.find({
foo : { "$all" : [ { "$elemMatch" : { bar : "xy", baz : 1} }, { "$elemMatch" : { bar : "a", baz : 10 } } ] }
});
I tried it with several other examples and it really works. But the official documentation for $all operator doesn't say anything about combining these two queries.
Is this the intended behavior or a bug? Or is this just a problem that the documentation does not cover this use case?