title: MongoDB query with elemMatch for nested array data
body: <p>I have a document containing nested array data. I tried hopelessly to filter the data using <code>$elemMatch</code> but I cannot figure out why it is not working.</p>
  
  <pre><code>{
  'id' : 1,
  'name' : 'test',
  'modules' : [
      {
          name: 'foo',
          mandatory: false,
          group: [
              {
                  name: g1
              }]
      },
      {
          name: 'bar',
          mandatory: false,
          group: [
              {
                  name: g2
              }]
      }]
  }
  </code></pre>
  
  <p>I tried using this query:</p>
  
  <pre><code>db.test.find(
  {
    modules: {
              $elemMatch: {
                   name: "foo",
              }
    }
  }
  </code></pre>
  
  <p>But it keeps returning all the modules. If I use <code>mandatory: true</code> it returns nothing, which seems to indicates it works. Any idea what am I doing wrong? Thanks!</p>