Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to duplicate a mongodb collection with the node.js driver?

Is there anyway to duplicate an collection through the nodejs mongodb driver?

i.e. collection.copyTo("duplicate_collection");

like image 846
Mathew Kurian Avatar asked Jul 25 '14 23:07

Mathew Kurian


1 Answers

You can eval copyTo() server-side though it will block the entire mongod process and won't create indexes on the new collection.

var copyTo = "function() { db['source'].copyTo('target') };"

db.eval(copyTo, [], function(err, result) {
  console.log(err);
});

Also note the field type warning.

"When using db.collection.copyTo() check field types to ensure that the operation does not remove type information from documents during the translation from BSON to JSON. Consider using cloneCollection() to maintain type fidelity."

like image 174
Justin Case Avatar answered Oct 30 '22 09:10

Justin Case