How to copy collections to new collections in same MongodB in MeteorJS


This is showing how to copy one from one collection to another in the same dB in Mongo shell

db.myoriginal.aggregate([ { $out: "mycopy" } ])

How to do this inside MeteorJS thanks.

Essentially I hope the new copied collections will have new id for further merging and MapReduce as otherwise merging cannot be done if IDs are the same.

MyCollection.aggregate <- this is not valid function inside MeteorJS?


You can either call aggregate on Mongo.Collection#rawCollection yourself (server side only), or use a third party package like meteorhacks:aggregate that does this for you.


Thanks Hugh, they both seem to be server side only, what if I need to create a copy of the original collection, say when button is clicked by user and wanting to generate a small local collection copy?


To handle this client side, you’ll have to copy the collection yourself. Something like:

const originalCollection = new Mongo.Collection('original_collection');
const copiedLocalCollection = new Mongo.Collection(null);
const copiedArrayWithoutIds = originalCollection.find().map((someObject) => {
  return _.omit(someObject, '_id');