18

I want to save the aggregation framework result to a new collection. I know that's impossible with the framework at the moment with the command itself.

Is there a workaround in the shell?

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
hotips
  • 2,575
  • 7
  • 42
  • 59

3 Answers3

48

Starting with Mongo 2.6.0 you can do this natively without any additional manipulation.

db.<collection>.aggregate( [
     { <operation> },
     { <operation> },
     ...,
     { $out : "<output-collection>" }
] )

Check the new aggregation operator $out for more detailed example.

P.S. using this way you are not limited to 16Mb size.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • So what to do you pre 2.6? I need to get around the 16Mb limit and still create a new collection using aggregation pipeline on 2.4. – conner.xyz Aug 15 '15 at 20:23
  • Again, need to get around 16mb limit though. Was working on a 2.4 instance. Used a `find()`, `foreach()`, `insert()` successfully. – conner.xyz Sep 24 '15 at 21:41
  • Related to the limits, is there still an upper limit of the data that can be written to the new collection in terms of size or document count? – Naman Mar 27 '20 at 19:17
23

Update: See Salvador's answer for a more efficient way to do this in MongoDB 2.6+.


Save the aggregation result in a variable and then insert its result property into a new collection:

var result = db.foo.aggregate(...);
db.bar.insert(result.result);
Community
  • 1
  • 1
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 3
    Only bummer is needing to hold all the result values in memory. See [here](https://jira.mongodb.org/browse/SERVER-3253) for the issue regarding `$out`. – Aidan Feldman Jun 24 '13 at 06:16
  • 2
    Also, that result set needs to fit in the standard MongoDB document size (16MB). – Aidan Feldman Jun 24 '13 at 06:24
  • @AidanFeldman: The Aggregation inline result is [already limited](http://docs.mongodb.org/manual/core/aggregation/#result) to the maximum BSON document size. – Stennie Jun 24 '13 at 07:19
  • 1
    @Stennie luckily not for long, 2.6 will address this. – Tom Swifty Oct 29 '13 at 15:33
  • I was able to do the following: db.foo.aggregate(...).forEach(function(it){db.bar.insert(it);}); I'm not sure if this is more efficient though. – taari Dec 04 '17 at 13:15
1

result.result is no longer working, try toArray().

var result  = db.coll.aggregate(...);
db.bar.insert(result.toArray());
ddsh79
  • 119
  • 5