Quantcast
Channel: mongodb $elemMatch - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Derick for mongodb $elemMatch

$
0
0

You can not use elemMatch for this, but you can simply just create a query which checks whether a matches the whole array:

db.items.insert({ 'foo' : 1, 'a' :  [{a:1, b:2},{a:3, b:4}, {a:5, b:6}]});db.items.insert({ 'foo' : 1, 'a' :  [{a:1, b:2},{a:3, b:4}, {a:8, b:7}]});db.items.find({'a': [{a:1, b:2},{a:3, b:4}, {a:8, b:7}]});{ "_id" : ObjectId("4f3391856e196eca5eaa7518"), "foo" : 1, "a" : [ { "a" : 1, "b" : 2 }, { "a" : 3, "b" : 4 }, { "a" : 8, "b" : 7 } ] }

However, for this to work the order of the elements in the array need to be the same for the document and the query. The following will not find anything:

db.items.find({'a': [{a:3, b:4},{a:1, b:2}, {a:8, b:7}]});

(Because {a:3, b:4} and {a:1, b:2} are swapped).


Viewing all articles
Browse latest Browse all 4

Trending Articles