MongoDB: How to merge two collections/databases together into one?

Starting Mongo 4.2, the new aggregation stage $merge can be used to merge the content of a collection in another collection in another database:

// > use db1
// > db.collection.find()
//   { "_id" : 1, "key" : "a", "value" : "b" }
//   { "_id" : 2, "key" : "c", "value" : "d" }
//   { "_id" : 3, "key" : "a", "value" : "b" }
// > use db2
// > db.collection.find()
//   { "_id" : 1, "key" : "e", "value" : "f" }
//   { "_id" : 4, "key" : "a", "value" : "b" }
// > use db1
db.collection.aggregate([
  { $merge: { into: { db: "db2", coll: "coll" } } }
])
// > use db2
// > db.collection.find()
//   { "_id" : 1, "key" : "a", "value" : "b" }
//   { "_id" : 2, "key" : "c", "value" : "d" }
//   { "_id" : 3, "key" : "a", "value" : "b" }
//   { "_id" : 4, "key" : "a", "value" : "b" }

By default, when the target and the source collections contain a document with the same _id, $merge will replace the document from the target collection with the document from the source collection. In order to customise this behaviour, check $merge's whenMatched parameter.


I think the easiest (and maybe the only) way is to write a script that merges the two databases document after document.

  1. Get first document from DB_B.
  2. Insert it into DB_A if needed.
  3. Delete it from DB_B.
  4. Repeat until done.

Instead of deleting documents from source db (DB_B), you may want to just read documents in batches. This should be more performant, but slightly more difficult to code (especially if you never done such a thing).