Skip to content

Copy/Clone MongoDB database

In this article I am going to show how one can clone a mongo DB database.

For this we will use tools like ‘mongodump’ and ‘mongorestore’. These are command line based tools and from MongoDB version 4.4, they are no longer part of MongoDB installation. For that you need to download them separately and use them.

To clone a database, first we need to create a dump of a database, and then restore it as a new database. Here I will clone a database named ‘test’ to a new database ‘copy-test’. You can find more information and additional functions on this on official MongoDB documentation.

To create a dump:

mongodump --archive="mongodump-test-db" --db=test

Now to restore it as new database:

mongorestore --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='copy-test.*.'

Another one line-er way is, instead of archive, we can pipe the dump data via standard output stream to restore and command will become:

mongodump --archive --db=test | mongorestore --archive --nsFrom='test.*' --nsTo='copy-test.*'

This will create a new database named ‘copy-test’.

Now here is the fun part and the reason to write this blog. Means you can always find these commands on MongoDB documentation. Then why write this?

It’s because I was running these commands on Windows operating system, and I was getting this error whenever I was running the restore command:

2021-04-04T12:06:40.172+0530 continuing through error: E11000 duplicate key error collection: test.orders index: id dup key: { id: ObjectId('605836d393d650cff0923321') } 2021-04-04T12:06:40.172+0530 continuing through error: E11000 duplicate key error collection: test.orders index: _id dup key: { id: ObjectId('605836d393d650cff0923324') } 2021-04-04T12:06:40.172+0530 continuing through error: E11000 duplicate key error collection: test.orders index: _id dup key: { id: ObjectId('605836d393d650cff0923327') } 2021-04-04T12:06:40.172+0530 continuing through error: E11000 duplicate key error collection: test.orders index: _id dup key: { _id: ObjectId('605836d393d650cff092332a') }
2021-04-04T12:06:40.172+0530 test.orders 50.4MB
2021-04-04T12:06:40.172+0530 no indexes to restore
2021-04-04T12:06:40.173+0530 finished restoring test.orders (0 documents, 9235 failures)
2021-04-04T12:06:40.173+0530 0 document(s) restored successfully. 9348 document(s) failed to restore.

It was wired! Means I am trying to create a new database and that is not existing yet, how there can be the duplicate keys?

After lots of searching, I didn’t found anything and just by luck I thought let me change the single quotes to double quotes (because similar kind of thing happened to me earlier with some other command line tool), and it worked.

mongodump --archive --db=test | mongorestore --archive --nsFrom="test.*" --nsTo="copy-test.*"

And result was:

2021-04-04T12:12:26.099+0530 no indexes to restore
2021-04-04T12:12:26.100+0530 finished restoring copy-test.orders (9235 documents, 0 failures)
2021-04-04T12:12:26.100+0530 9348 document(s) restored successfully. 0 document(s) failed to restore.

It seems silly thing, but just to keep in mind for next time.

Be First to Comment

Leave a Reply

Your email address will not be published.