Mongodb – Save Subset of MongoDB Collection to Another Collection


I have a set like so

{date: 20120101}
{date: 20120103}
{date: 20120104}
{date: 20120005}
{date: 20120105}

How do I save a subset of those documents with the date '20120105' to another collection?

i.e{date: "20120105"}));

Best Solution

As a newer solution I would advise to use Aggregation framework for the problem:

db.full_set.aggregate([ { $match: { date: "20120105" } }, { $out: "subset" } ]);

It works about 100 times faster than forEach at least in my case. This is because the entire aggregation pipeline runs in the mongod process, whereas a solution based on find() and insert() has to send all of the documents from the server to the client and then back. This has a performance penalty, even if the server and client are on the same machine.