2

I have been reading through old answers here to try to find a solution to this - but I haven't been successful.

I have a field in a document that has a nested array as follows:

[tags] => Array ( 
       [0] => Array ( [_id] => 5271318ea7d63ba9a25045ad 
                      [name] => photo ) 
       [1] => Array ( [_id] => 526ffafba7d63ba9a25045a9 
                      [name] => beach ) 
       [2] => Array ( [_id] => 5271318ea7d63ba9a25045ac 
                      [name] => landscape ) 
)

I'm looking for documents that have all of the required tag id's in the search query. I have this query, but it returns all of the documents that have any of the id's.

{"tags":{"$elemMatch":{"_id":{"$in":["5271318ea7d63ba9a25045ad","526ffafba7d63ba9a25045a9"]}}}}

If I change the query from $in to $all it does not return anything.

Any help would be appreciated.

0

1 Answer 1

4

Try this. Queries using $in and $all don't quite work the same.

{"tags": { $all: [
    {"$elemMatch": {"_id": "5271318ea7d63ba9a25045ad"}},
    {"$elemMatch": {"_id": "526ffafba7d63ba9a25045a9"}}]
}}

Here's an example

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

Comments

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.