16

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?

1 Answer 1

8

This is the intended behavior. The documentation doesn't cover this use case and we are working on it to make it better. Its difficult, however, to document every possible query combination.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank for the info. I understand that it's hard to document every combination but it would be very useful to know that some of the operators can be combined.
Is there any way to do this yet?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.