Reputation: 597
Is there a way to Clone a MongoDB database and its data with MongoDB Compass to my local server? I do not want to corrupt the dev environment by accident so I want to test things out locally, but I can't find a solution for that on MongoDB Compass.
Upvotes: 8
Views: 18145
Reputation: 546
As pointed out for Gustavo Garcia, MongodDB is not very friendly for this, and so far I was able to clone and export. But is not very well documented and I posted my recent answer here (mongodb want export collections missing semicolon)
Basically I did: mongodump --archive="<the-new-db-name>" --db=<the-db-to-copy>--host="localhost" --port=<e.g:27017> --username=<your-username> --authenticationDatabase=admin
from my shell (replace the values between triangle brackets with your own details) .
Note: If you want it as an output file (instead of archived one), you will need to change the "archive=" statement by "--out="
Note2: I am ussing MongoDB: 7.0.5 and Mongosh: 2.1.4
Upvotes: 0
Reputation: 59
To clone an existing database to a new one, use the mongodump
and mongorestore
commands.
To dump DB:
mongodump -d <database_name> -o <directory_backup>
To clone DB:
mongorestore -d <new_database_name> <directory_backup>/<database_name>
Upvotes: 4
Reputation: 1690
MongoDB Compass has a great documentation explaining how you can import/export one collection at the time for both JSON and CSV files.
I think that there is no better answer than the link to the documentation: https://docs.mongodb.com/compass/current/import-export#import-data-into-a-collection
Upvotes: 5