title: mongodb select sub document on the basis of search result of parent document
body: <p>I have a <code>chats</code> collection witch sample document</p>
  
  <pre><code>{
      "_id" : ObjectId("57f95e8e385bb61c5cf2cd18"),
      //some other fields
      "messages" : [
              {
                      "sender" : "57ec1aaa0ffe16123439d52b",
                      "message" : "Hello!",
                      "sentTime" : "Oct 13, 2016 1:51:31 AM"
              },
              {
                      "sender" : "57ec1aaa0ffe16123439d52b",
                      "message" : "Hello!",
                      "sentTime" : "Oct 13, 2016 1:51:33 AM"
              }
      ]
  }
  </code></pre>
  
  <p>I want to search the collection on the basis of <code>_id</code> field and only get the data of the <code>messages sub-document</code> as the query result using java driver for MongoDB as I want to Map to POJO array using Gson.</p>
  
  <p>Desired Output:</p>
  
  <pre><code>[
          {
                  "sender" : "57ec1aaa0ffe16123439d52b",
                  "message" : "Hello!",
                  "sentTime" : "Oct 13, 2016 1:51:31 AM"
          },
          {
                  "sender" : "57ec1aaa0ffe16123439d52b",
                  "message" : "Hello!",
                  "sentTime" : "Oct 13, 2016 1:51:33 AM"
          }
  ]
  </code></pre>
  
  <p>How do I go with this using Java driver for MongoDB</p>