Export mongodb aggregation framework result to a new collection

前端 未结 3 1418
心在旅途
心在旅途 2020-12-03 00:01

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 w

相关标签:
3条回答
  • 2020-12-03 00:20

    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.

    0 讨论(0)
  • 2020-12-03 00:37

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

    var result  = db.coll.aggregate(...);
    db.bar.insert(result.toArray());
    
    0 讨论(0)
  • 2020-12-03 00:41

    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);
    
    0 讨论(0)
提交回复
热议问题