combining two $elemMatch queries with AND boolean operator in MongoDB
我有两个不同的 mongoDB 查询,代表两个不同的条件,例如:
1 | { stuff: { $elemMatch: { foo: 1, bar:"a" } } } |
和:
1 | { stuff: { $elemMatch: { foo: 2, bar:"b" } } } |
其中
现在,我不确定如何匹配集合中同时满足上述两个条件的元素。
要明确一点:在这种情况下,我需要获取所有元素,这些元素同时具有
执行
你知道如何
Just to be clear: in this case I need to get all elements that have both one element of stuff that has foo set as 1 with bar set as"a" and also one element of stuff that has foo set as 2 with bar set as"b".
我希望我明白了。那不应该是
1 2 3 | db.coll.find({ $and: [ {"stuff" : { $elemMatch : {"foo" : 1,"bar" :"a" } } }, {"stuff" : { $elemMatch : {"foo" : 2,"bar" :"b" } } } ]}); |
?