Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export mongodb aggregation framework result to a new collection

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?

like image 972
hotips Avatar asked Nov 28 '12 18:11

hotips


People also ask

What does MongoDB aggregation return?

aggregate() method returns a cursor to the documents produced by the final stage of the aggregation pipeline operation, or if you include the explain option, the document that provides details on the processing of the aggregation operation.

What is unwind in MongoDB aggregation?

What is MongoDB $unwind? The MongoDB $unwind operator is used to deconstruct an array field in a document and create separate output documents for each item in the array.

What does Mongoose aggregate return?

Mongoose Aggregate Class Mongoose's aggregate() function returns an instance of Mongoose's Aggregate class. Aggregate instances are thenable, so you can use them with await and promise chaining. The Aggregate class also supports a chaining interface for building aggregation pipelines.


1 Answers

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.

like image 92
Salvador Dali Avatar answered Oct 21 '22 14:10

Salvador Dali