title: query to retrieve multiple objects in an array in mongodb
body: <p>Suppose I have a array of objects as below.</p>
  
  <pre><code>"array" : [ 
      {
          "id" : 1
      }, 
      {
          "id" : 2
      }, 
      {
          "id" : 2
      }, 
      {
          "id" : 4
      }
  ]
  </code></pre>
  
  <p>If I want to retrieve multiple objects (<strong>{id : 2}</strong>) from this array, the aggregation query goes like this.</p>
  
  <pre><code>db.coll.aggregate([{ $match : {"_id" : ObjectId("5492690f72ae469b0e37b61c")}}, { $unwind : "$array"}, { $match : { "array.id" : 2}}, { $group : { _id : "$_id", array : { $push : { id : "$array.id"}}}}  ])
  </code></pre>
  
  <p>The output of above aggregation is </p>
  
  <pre><code>   {
          "_id" : ObjectId("5492690f72ae469b0e37b61c"),
          "array" : [ 
              {
                  "id" : 2
              }, 
              {
                  "id" : 2
              }
          ]
      }
  </code></pre>
  
  <p>Now the question is:
  1) Is retrieving of <strong>multiple objects</strong> from an array possible using <strong>find()</strong> in MongoDB?</p>
  
  <p>2) With respect to <strong>performance</strong>, is aggregation is the correct way to do? (Because we need to use <strong>four pipeline operators</strong>) ?</p>
  
  <p>3) Can we use Java manipulation (looping the array and only keep {id : 2} objects) to do this after 
  <strong>find({"_id" : ObjectId("5492690f72ae469b0e37b61c")})</strong> query? Because find will once retrieve the document and keeps it in RAM. But if we use aggregation four operations need to be performed in RAM to get the output. </p>
  
  <p>Why I asked the 3) question is: Suppose if thousands of clients accessing at the same time, then RAM memory will be overloaded. If it is done using Java, less task on RAM.</p>
  
  <p>4) <strong>For how long the workingSet will be in RAM</strong>??</p>
  
  <p>Is my understanding correct???</p>
  
  <p>Please correct me if I am wrong.</p>
  
  <p>Please suggest me to have right insight on this..</p>