title: How to write a Aggregation Pipeline to get a merged document as new field on document insert MongoDB?
body: <p>I'm newbie on MongoDB and I'll show my question with a example. Scenario:</p>
  <ul>
  <li>3 Collections;</li>
  </ul>
  <p><strong>Collection_1</strong></p>
  <pre><code>{
      &quot;_id&quot;: 1,
      &quot;idLocal&quot;: 1023,
      &quot;idType&quot;: 21 
  },
  {
      &quot;_id&quot;: 2,
      &quot;idLocal&quot;: 1029,
      &quot;idType&quot;: 21 
  },
  {
      &quot;_id&quot;: 3,
      &quot;idLocal&quot;: 923,
      &quot;idType&quot;: 22 
  }
  </code></pre>
  <p><strong>Collection_2</strong></p>
  <pre><code>{
      &quot;_id&quot;: 1,
      &quot;idLOp&quot;: 1,
      &quot;idType&quot;: 21    
  },
  {
      &quot;_id&quot;: 2,
      &quot;idOp&quot;: 5,
      &quot;idType&quot;: 21 
  }
  {
      &quot;_id&quot;: 3,
      &quot;idOp&quot;: 1,
      &quot;idType&quot;: 12 
  }
  </code></pre>
  <p>So, to insert a new document on Collection_3 I want create a new doc appending the docs from Collection_1 and Collection_2 with the follow conditions:</p>
  <pre><code>idLocal (Collection_1) == idLocal (New doc to insert) // get desired doc from Collection_1
  idOp (Collection_2) == idOp (New doc to insert) AND idType (Collection_1 ) == idType  (Collection_2) // get desired doc from Collection_2
  </code></pre>
  <p><strong>Sample doc to insert</strong></p>
  <pre><code>{
      &quot;_id&quot;: 5,
      &quot;idOp&quot;: 1,
      &quot;idLocal&quot;: 1023
  }
  </code></pre>
  <p><strong>Collection_3</strong></p>
  <pre><code>// new doc saved at Collection_3 result desired:
  
  {
      &quot;_id&quot;: 5,
      &quot;idOP&quot;: 1,
      &quot;idLocal&quot;: 1023,
      &quot;Collection_1_doc&quot;: { 
            &quot;_id&quot;: 1,
            &quot;idLocal&quot;: 1023,
            &quot;idType&quot;: 21  
      },
      &quot;Collection_2_doc&quot;: {
             &quot;_id&quot;: 1,
             &quot;idLOp&quot;: 1,
             &quot;idType&quot;: 21 
      }
  }
  </code></pre>
  <p>So, how I can use the MongoDB pipeline tools to make it? I never writed a pipeline before and don't know use the functions.</p>