title: How to filter array in subdocument with MongoDB
body: <p>I have array in subdocument like this</p>
  
  <pre><code>{
      "_id" : ObjectId("512e28984815cbfcb21646a7"),
      "list" : [
          {
              "a" : 1
          },
          {
              "a" : 2
          },
          {
              "a" : 3
          },
          {
              "a" : 4
          },
          {
              "a" : 5
          }
      ]
  }
  </code></pre>
  
  <p>Can I filter subdocument for a > 3</p>
  
  <p>My expect result below</p>
  
  <pre><code>{
      "_id" : ObjectId("512e28984815cbfcb21646a7"),
      "list" : [
          {
              "a" : 4
          },
          {
              "a" : 5
          }
      ]
  }
  </code></pre>
  
  <p>I try to use <code>$elemMatch</code> but returns the first matching element in the array</p>
  
  <p>My query:</p>
  
  <pre><code>db.test.find( { _id" : ObjectId("512e28984815cbfcb21646a7") }, { 
      list: { 
          $elemMatch: 
              { a: { $gt:3 } 
              } 
      } 
  } )
  </code></pre>
  
  <p>The result return one element in array</p>
  
  <pre><code>{ "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 4 } ] }
  </code></pre>
  
  <p>and I try to use aggregate with <code>$match</code> but not work</p>
  
  <pre><code>db.test.aggregate({$match:{_id:ObjectId("512e28984815cbfcb21646a7"), 'list.a':{$gte:5}  }})
  </code></pre>
  
  <p>It's return all element in array</p>
  
  <pre><code>{
      "_id" : ObjectId("512e28984815cbfcb21646a7"),
      "list" : [
          {
              "a" : 1
          },
          {
              "a" : 2
          },
          {
              "a" : 3
          },
          {
              "a" : 4
          },
          {
              "a" : 5
          }
      ]
  }
  </code></pre>
  
  <p>Can I filter element in array to get result as expect result?</p>