title: mongodb custom field with other query
body: <p>I have a collection with this structure:</p>
  
  <pre><code>{
   _id : &lt;number&gt;,
   param1 : &lt;number&gt;,
   param2 : &lt;number&gt;,
   param3 : &lt;number&gt;,
   .
   .
   .
  }
  </code></pre>
  
  <p>I would like to get the documents back which are right for the conditions and contains an extra array parameter with the _id values of those documents which have the same value in "param1" as the actual row has. So I'd a result like this:</p>
  
  <pre><code>{
   _id : &lt;number&gt;,
   param1 : &lt;number&gt;,
   param2 : &lt;number&gt;,
   param3 : &lt;number&gt;,
   .
   .
   .
   extraParameter: [{_id: 1}, {_id: 5}, {_id: 7}, {_id: 10} ...]
  }
  </code></pre>
  
  <p>I'm not sure if I can do it but hope you can help me find the best solution for this.</p>
  
  <p>Thanks!</p>
  
  <p>Edited:</p>
  
  <p>I have already made the following query for this but the documentation says don't touch the database in the map but I don't know is there an other way or not ... or is there an easier one?:</p>
  
  <pre><code>db.collection.find().forEach(function(row){
    row.nexts = db.collection.find({param1: row.param1}).map(function(nextElement){
       return {_id: nextElement._id};});
    return row;}
  )
  </code></pre>