title: Why MongoDB $elemMatch returns all elements in array?
body: <p>In mongoDB (2.4) I have collection <code>students</code> with bunch of documents in it. Each document will have structure as below:</p>
  
  <pre><code>{
      "_id" : ObjectId("55f16640a17eed480bb0dc18"),
      "messages" : [
          {
              "_id" : ObjectId("55fc60bc260c9be5879ef733"),
              "createdOn" : ISODate("2015-09-18T19:06:36.793Z"),
              "fromUser" : ObjectId("55f16a6af9c703280c5b2c0e"),
              "toUser" : ObjectId("55f16640a17eed480bb0dc18"),
              "type" : 1
          },
          {
              "_id" : ObjectId("55fc6d200d6097e98b210bff"),
              "createdOn" : ISODate("2015-09-18T19:59:28.439Z"),
              "fromUser" : ObjectId("55f721d1e1242e7c4c129471"),
              "toUser" : ObjectId("55f16640a17eed480bb0dc18"),
              "type" : 1
          }
      ]
  }
  </code></pre>
  
  <p>Now, I need to pull out one of the subdocuments from messages array, say with <code>messages._id</code> equal <code>55fc60bc260c9be5879ef733</code> so I use <code>$elemMatch</code>:</p>
  
  <pre><code>db.users.find({"_id": new ObjectId('55f16640a17eed480bb0dc18'),
                  "messages": {
                      $elemMatch:{
                          "_id": new ObjectId('55fc60bc260c9be5879ef733'),
                          "fromUser": new ObjectId('55f16a6af9c703280c5b2c0e'),
                          "toUser": new ObjectId('55f16640a17eed480bb0dc18'),
                          "type": 1
  
                  }}}).pretty();
  </code></pre>
  
  <p>But this gives me:</p>
  
  <pre><code>{
      "_id" : ObjectId("55f16640a17eed480bb0dc18"),
      "messages" : [
          {
              "_id" : ObjectId("55fc60bc260c9be5879ef733"),
              "createdOn" : ISODate("2015-09-18T19:06:36.793Z"),
              "fromUser" : ObjectId("55f16a6af9c703280c5b2c0e"),
              "toUser" : ObjectId("55f16640a17eed480bb0dc18"),
              "type" : 1
          },
          {
              "_id" : ObjectId("55fc6d200d6097e98b210bff"),
              "createdOn" : ISODate("2015-09-18T19:59:28.439Z"),
              "fromUser" : ObjectId("55f721d1e1242e7c4c129471"),
              "toUser" : ObjectId("55f16640a17eed480bb0dc18"),
              "type" : 1
          }
      ]
  }
  </code></pre>
  
  <p>If I understand docs correctly <a href="http://docs.mongodb.org/v2.4/reference/operator/projection/elemMatch/" rel="nofollow">http://docs.mongodb.org/v2.4/reference/operator/projection/elemMatch/</a> I should receive only:</p>
  
  <pre><code>{
      "_id" : ObjectId("55f16640a17eed480bb0dc18"),
      "messages" : [
          {
              "_id" : ObjectId("55fc60bc260c9be5879ef733"),
              "createdOn" : ISODate("2015-09-18T19:06:36.793Z"),
              "fromUser" : ObjectId("55f16a6af9c703280c5b2c0e"),
              "toUser" : ObjectId("55f16640a17eed480bb0dc18"),
              "type" : 1
          }
      ]
  }
  </code></pre>
  
  <p>So what am I doing wrong?</p>