title: Merge two collections in MongoDB
body: <p>I use MongoDB for several months but with basic usage, and it's maybe the first time that I try to use more complicated functions.</p>
  
  <p>I have two collections :</p>
  
  <p>collection 'trades' :</p>
  
  <pre><code>{ "_id" : ObjectId("5878ec66ad251f4fb4d2aacc"), "clubCode" : "43W0K", "date" : ISODate("2017-01-13T15:03:52.410Z"), "symbol" : "ENGI.PA", "name" : "ENGIE SA", "buyOrSell" : "buy", "orderDone" : true, "quantity" : 12, "price" : 11.99, "fees" : 0.99, "total" : 144.87, "__v" : 0 }
  { "_id" : ObjectId("5878f8c339b47f0ee4a3b80b"), "clubCode" : "43W0K", "date" : ISODate("2017-01-13T15:56:32.088Z"), "symbol" : "PRIO.PA", "name" : "Lyxor ETF PEA Brazil (Ibovespa) C-EUR", "buyOrSell" : "buy", "orderDone" : true, "quantity" : 56, "price" : 8.92, "fees" : 0.99, "total" : 500.51, "__v" : 0 }
  { "_id" : ObjectId("5878fadf39b47f0ee4a3b80c"), "clubCode" : "43W0K", "date" : ISODate("2017-01-13T16:05:35.849Z"), "symbol" : "ALVIV.PA", "name" : "Visiativ SA", "buyOrSell" : "buy", "orderDone" : true, "quantity" : 10, "price" : 18.15, "fees" : 0.99, "total" : 182.49, "__v" : 0 }
  { "_id" : ObjectId("587a03319e3fe23138119937"), "clubCode" : "43W0K", "date" : ISODate("2017-01-14T10:52:56.208Z"), "symbol" : "BIG.PA", "name" : "BigBen Interactive", "buyOrSell" : "buy", "orderDone" : false, "orderType" : "ACL", "quantity" : 83, "price" : 6.01, "fees" : 0.99, "total" : 499.82, "__v" : 0 }
  </code></pre>
  
  <p>collection 'subscriptions' :</p>
  
  <pre><code>{ "_id" : ObjectId("587c946f3aa3f229a0922761"), "email" : "aaa@aaa.fr", "clubCode" : "43W0K", "period" : ISODate("2016-09-01T22:00:00Z"), "amount" : 100, "type" : "recurrent", "__v" : 0 }
  { "_id" : ObjectId("587c946f3aa3f229a0922762"), "email" : "bbb@bbb.fr", "clubCode" : "43W0K", "period" : ISODate("2016-09-01T22:00:00Z"), "amount" : 100, "type" : "recurrent", "__v" : 0 }
  { "_id" : ObjectId("587c946f3aa3f229a0922763"), "email" : "ccc@ccc.fr", "clubCode" : "43W0K", "period" : ISODate("2016-09-01T22:00:00Z"), "amount" : 100, "type" : "recurrent", "__v" : 0 }
  </code></pre>
  
  <p>Each are related to a clubCode (here '43W0K').</p>
  
  <p>I would like to have this result (in another collection or just with an aggregate) :</p>
  
  <pre><code>{
      clubCode: "43W0K",
      treasury_moves: [
          {
              date: ISODate("2017-01-13T15:03:52.410Z"),
              wording: "Achat ENGIE SA",
              amount: 144.87
          }, {
              date: ISODate("2017-01-13T15:56:32.088Z"),
              wording: "Achat Lyxor ETF PEA Brazil (Ibovespa) C-EUR",
              amount: 500.51
          }, {
              date: ISODate("2017-01-13T16:05:35.849Z"),
              wording: "Achat Visiativ SA",
              amount: 182.49
          }, {
              date: ISODate("2017-01-14T10:52:56.208Z"),
              wording: "Achat BigBen Interactive",
              amount: 499.82
          }, {
              date: ISODate("2016-09-01T22:00:00Z"),
              wording: "Subscription aaa@aaa.fr",
              amount: 100
          }, {
              date: ISODate("2016-09-01T22:00:00Z"),
              wording: "Subscription bbb@bbb.fr",
              amount: 100
          }, {
              date: ISODate("2016-09-01T22:00:00Z"),
              wording: "Subscription ccc@ccc.fr",
              amount: 100
          }
      ]
  }
  </code></pre>
  
  <p>So a merge between 'trades' and 'subscriptions' collections, </p>
  
  <ul>
  <li>which <code>date</code> is equal to <code>$date</code> for 'trades' or <code>$period</code> for 'subscriptions'</li>
  <li>which <code>wording</code> is equal to <code>Achat $name</code> for 'trades', or <code>Subscription $email</code> for 'subscriptions'</li>
  <li>which <code>amount</code> result is equal to <code>$total</code> if it's in 'trades', or <code>$amount</code> if it's in 'subscriptions'</li>
  </ul>
  
  <p>First of all, is it possible to do this ? With mapreduce ? aggregate ?</p>
  
  <p>I've looking to <a href="https://stackoverflow.com/questions/9696940/merging-two-collections-in-mongodb">this topic</a> which seems similar but I don't succeed to have the result that I want.</p>
  
  <p>If you can help me or just indicate me the way to follow.</p>