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 db.subset.save(db.full_set.find({date: "20120105"}));
Best Answer
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()
andinsert()
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.