0

I'm wondering if anyone else have run into this problem in MongoDB.

I'm trying to search an array inside another array with $elemMatch and then see if a nested value inside of this document, is in an array of values I provide.

Below there is a simple example:

db.p.insert({v: [{o: [{s: {e: ["M"] }} ]} ]});
db.p.findOne({"v.o": {"$all": [{"$elemMatch": {"s.e": {"$in": ["M"]}}}]}});
null

Note, the reason for the $all statement is that I want to be able to match several different fields, where each field doesn't necessarily exist on the documents in the array.

CristiC
  • 22,068
  • 12
  • 57
  • 89
Archan
  • 1
  • 1

1 Answers1

0

$all is used to match an array property against a list of discrete values, not a list of conditions with operators like $elemMatch. If you remove the $all it works:

db.p.findOne({"v.o": { $elemMatch: { "s.e": {"$in": ["M"]}}}});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • According to: [link](http://stackoverflow.com/questions/9824341/multiple-elemmatch-expressions-for-matching-array-values-using-all-in-mongodb) it does support matching using $elemMatch.. – Archan Sep 20 '12 at 22:11
  • Interesting...undocumented feature! – JohnnyHK Sep 20 '12 at 22:24