How can documents be moved from one collection to another collection in MongoDB?? For example: I have lot of documents in collection A and I want to move all 1 month older documents to collection B (these 1 month older documents should not be in collection A).
Using aggregation we can do copy. But what I am trying to do is moving of documents. What method can be used to move documents?
MongoDB – copyTo() Method In MongoDB, copyTo() method is used to copies all the documents from one collection(Source collection) to another collection(Target collection) using server-side JavaScript and if that other collection(Target collection) is not present then MongoDB creates a new collection with that name.
@Naman what is the use case of copy collection, i mean you need any command or it is ok with manually process? for the manual process just install studio3T connect both databases and right click on collection that you want to copy, click on option "Copy Collection" and then go to second database right click on " ...
MongoDB is not a relational database, but you can perform a left outer join by using the $lookup stage. The $lookup stage lets you specify which collection you want to join with the current collection, and which fields that should match.
The bulk operations @markus-w-mahlberg showed (and @mark-mullin refined) are efficient but unsafe as written. If the bulkInsert fails, the bulkRemove will still continue. To make sure you don't lose any records when moving, use this instead:
function insertBatch(collection, documents) { var bulkInsert = collection.initializeUnorderedBulkOp(); var insertedIds = []; var id; documents.forEach(function(doc) { id = doc._id; // Insert without raising an error for duplicates bulkInsert.find({_id: id}).upsert().replaceOne(doc); insertedIds.push(id); }); bulkInsert.execute(); return insertedIds; } function deleteBatch(collection, documents) { var bulkRemove = collection.initializeUnorderedBulkOp(); documents.forEach(function(doc) { bulkRemove.find({_id: doc._id}).removeOne(); }); bulkRemove.execute(); } function moveDocuments(sourceCollection, targetCollection, filter, batchSize) { print("Moving " + sourceCollection.find(filter).count() + " documents from " + sourceCollection + " to " + targetCollection); var count; while ((count = sourceCollection.find(filter).count()) > 0) { print(count + " documents remaining"); sourceDocs = sourceCollection.find(filter).limit(batchSize); idsOfCopiedDocs = insertBatch(targetCollection, sourceDocs); targetDocs = targetCollection.find({_id: {$in: idsOfCopiedDocs}}); deleteBatch(sourceCollection, targetDocs); } print("Done!") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With