title: How to generate object ids when $unwinding with aggregate in mongodb
body: <p>I'm having the following query</p>
  <pre><code>db.getCollection('matches').aggregate([{
      &quot;$lookup&quot;: {
        &quot;from&quot;: &quot;player&quot;,
        &quot;localField&quot;: &quot;players.account_id&quot;,
        &quot;foreignField&quot;: &quot;account_id&quot;,
        &quot;as&quot;: &quot;players2&quot;
      }
    }, {
      &quot;$addFields&quot;: {
        &quot;players&quot;: {
          &quot;$map&quot;: {
            &quot;input&quot;: &quot;$players&quot;,
            &quot;in&quot;: {
              &quot;$mergeObjects&quot;: [
                &quot;$$this&quot;, {
                  &quot;$arrayElemAt&quot;: [
                    &quot;$players2&quot;, {
                      &quot;$indexOfArray&quot;: [
                        &quot;$players.account_id&quot;,
                        &quot;$$this.account_id&quot;
                      ]
                    }
                  ]
                }
              ]
            }
          }
        }
      }
    }, {
      &quot;$set&quot;: {
        &quot;players.match_id&quot;: &quot;$match_id&quot;,
        &quot;players.radiant_win&quot;: &quot;$radiant_win&quot;
      }
    }, {
      &quot;$unwind&quot;: &quot;$players&quot;
    }, {
      &quot;$replaceRoot&quot;: {
        &quot;newRoot&quot;: &quot;$players&quot;
      }
    }, {
        &quot;$project&quot;: {
          &quot;_id&quot;: 1,
          &quot;match_id&quot;: 1,
          &quot;account_id&quot;: 1,
          &quot;hero_id&quot;: 1,
          &quot;radiant_win&quot;: 1
         }
      }
  ])
  </code></pre>
  <p>which is supposed to match an inner array with another collection, merge the objects in the arrays by the matching and then unwrap ($unwind) the array into a new collection.</p>
  <p>Unfortunately, I'm getting duplicate Object ids which is sort of a problem for when I want to export this collection.</p>
  <p>How can I ensure unique Object_Ids for the aggregation?</p>
  <p>Thanks in advance!</p>