Saving MongoDB query result

When doing research in the mongo shell, I often write quite complex queries and want the result to be saved in another collection. I know a way to do this with .forEach() :

db.documents.find(query).forEach(function(d){db.results.insert(d)})

But it is somehow tedious to write this material every time. Is there a cleaner way? I would like the syntax to be something like db.documents.find(query).dumpTo('collectionName') .

+6
source share
4 answers

Here is the solution I will use: db.results.insert(db.docs.find(...).toArray())

However, there is still too much noise.

UPD: it is also possible to rewrite find using the aggregation pipeline. Then you can use the $out operator .

+6
source

As far as I know, there are no built-in functions in MongoDB.

Other options: mongoexport / mongoimport or mongodump / mongorestore functionality.

In mongoexport and mongodump, you can filter the results by adding query parameters using --query <JSON> or -q <JSON> .

+1
source

It looks like you are making your requests from the mongo shell, which allows you to write code in javascript. You can assign the result of the queries to a variable:

 result = db.mycollection.findOne(my_query) 

And save the result in another collection:

 db.result.save(result) 

You may need to remove the _id of the result if you want to add it to the collection of results to prevent duplicate key error

Edit:

 db.mycollection.findOne({'_id':db.mycollection.findOne()['_id']}) db.foo.save(db.bar.findOne(...)) 

If you want to save an array, you can write a javascript function. Something like the following should work (I have not tested it):

 function save_array(arr) { for(var i = 0; i < arr.length; i++) { db.result.save(arr[i]) } } ... result = db.mycollection.find(...) save_array(result) 

If you want the function to be available every time you start the mongo shell, you can include it in your .mongorc.js file

+1
source

If your query uses an aggregation operator, then the solution is a sample using $ out.

I created a Collection sample called "tester" that contains the following entries.

 { "_id" : ObjectId("4fb36bfd3d1c88bfa15103b1"), "name" : "bob", "value" : 5, "state" : "b"} { "_id" : ObjectId("4fb36c033d1c88bfa15103b2"), "name" : "bob", "value" : 3, "state" : "a"} { "_id" : ObjectId("4fb36c063d1c88bfa15103b3"), "name" : "bob", "value" : 7, "state" : "a"} { "_id" : ObjectId("4fb36c0c3d1c88bfa1a03b4"), "name" : "john", "value" : 2, "state" : "a"} { "_id" : ObjectId("4fb36c103d1c88bfa5103b5"), "name" : "john", "value" : 4, "state" : "b"} { "_id" : ObjectId("4fb36c143d1c88bfa15103b"), "name" : "john", "value" : 8, "state" : "b"} { "_id" : ObjectId("4fb36c163d1c88bfa15103a"), "name" : "john", "value" : 6, "state" : "a"} 

Now, using the aggregate operator, I execute the group, and then save the result to a new collection using this magic "$ out" operator.

 db.tester.aggregate([{$group:{ _id:{name:"$name",state:"$state"}, min:{$min:"$value"}, max:{$max:"$value"}, } }, {$out:"tester_max_min"} ]) 

What basically tries to execute the query is to group by name and state and find the minimum and maximum values ​​for each individual group, and then save the result in a new collection called "tester_max_min"

 db.tester_max_min.find(); 

The newly formed collection will contain the following documents:

 { "_id" : { "name" : "john", "state" : "b" }, "min" : 4, "max" : 8 } { "_id" : { "name" : "john", "state" : "a" }, "min" : 2, "max" : 6 } { "_id" : { "name" : "bob", "state" : "a" }, "min" : 3, "max" : 7 } { "_id" : { "name" : "bob", "state" : "b" }, "min" : 5, "max" : 5 } 

I still need to learn how useful it is to use $ out, but it works like a charm for any aggregator operator.

+1
source

All Articles