Reputation: 421
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.
Upvotes: 2
Views: 1591
Reputation: 7464
Try this. Queries using $in
and $all
don't quite work the same.
{"tags": { $all: [
{"$elemMatch": {"_id": "5271318ea7d63ba9a25045ad"}},
{"$elemMatch": {"_id": "526ffafba7d63ba9a25045a9"}}]
}}
Upvotes: 4