Is there a better way to export a mongodb query to a new collection?

Viewed 3020

What I want: I have a master collection of products, I then want to filter them and put them in a separate collection.

db.masterproducts.find({category:"scuba gear"}).copyTo(db.newcollection)

Of course, I realise the 'copyTo' does not exist.

I thought I could do it with MapReduce as results are created in a new collection using the new 'out' parameter in v1.8; however this new collection is not a subset of my original collection. Or can it be if I use MapReduce correctly?

To get around it I am currently doing this: Step 1: /usr/local/mongodb/bin/mongodump --db database --collection masterproducts -q '{category:"scuba gear"}'

Step 2: /usr/local/mongodb/bin/mongorestore -d database -c newcollection --drop packages.bson

My 2 step method just seems rather inefficient!

Any help greatly appreciated.

Thanks

Bob

4 Answers
Related