How to aggregate and merge the result into a collection?

mongodb

Solution

I used the output from aggregation to insert/merge to collection:

    db.coll2.insert(
      db.coll1.aggregate([]).toArray()
    )

Problem

I want to aggregate and insert the results into an existing collection, without deleting that collection. The documentation seems to suggest that this isn't directly possible. I find that hard to believe. The map-reduce functionality has 'output modes', including 'merge', which does what I want. I'm looking for the equivalent for aggregation. The new `$out` aggregation stage supports inserting into a collection, but it replaces the collection rather than updating it. If I did this I would (I think) have to run another map-reduce to merge this into another collection, which seems inefficient. Am I missing something or is the functionality just missing from the aggregation feature?

Original source