Answer a question

I have two databases named: DB_A and DB_B.

Each database has one collection with same name called store.

Both collections have lots lots of documents that have exactly the same structure { key:" key1", value: "value1" }, etc.

Actually, I was supposed to only create DB_A and insert all documents into DB_A. But later when I did my second round of inserting, I made a mistake by typing the wrong name as the database name.

So now, each database has a size of 32GB, I wish to merge two databases.

One problem/constraint is that the free space available now is only 15GB, so I can't just copy all things from DB_B to DB_A.

I am wondering if I can perform some kind of "move" to merge the two databases? I prefer the most efficient way as simply reinserting 32GB into DB_A will take quite a time.

Answers

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).

Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐