title: MongoDB query help: $elemMatch in nested objects
body: <pre><code>&gt; db.test.insert({"a" : { "b" : { "c" : { "d1" : [ "e1" ],
                                            "d2" : [ "e2" ], 
                                            "d3" : [ "e3", "e4" ], 
                                            "d4" : [ "e5", "e6" ] } } } })
  &gt; db.test.find({'a.b.c' : {$exists : true}})
  { "_id" : ObjectId("4daf2ccd697ebaacb10976ec"), "a" : { "b" : { "c" : { "d1" : [ "e1" ], "d2" : [ "e2" ], "d3" : [ "e3", "e4" ], "d4" : [ "e5", "e6" ] } } } }
  </code></pre>
  
  <p>But none of these work:</p>
  
  <pre><code>&gt; db.test.find({'a.b': "c"})
  &gt; db.test.find({'a.b': {$elemMatch : {"c" : {$exists: true}}}})
  &gt; db.test.find({'a.b': {$elemMatch : {$elemMatch : {$all : ["e1"] }}}})
  </code></pre>
  
  <p>Suppose I don't know what the values of <code>c</code> and <code>d1</code>...<code>d4</code> are. Is there a generic way to search the nested-objects' structure for particular values? </p>
  
  <p>I thought that was what <code>$elemMatch</code> was for.</p>
  
  <p>Thank you.</p>