Cloning MongoDB database with MongoDB Compass
Asked Answered
G

2

6

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.

Gayden answered 8/1, 2021 at 14:29 Comment(1)
There is no way. It only uses JSON/CSV files, but the conversion may break or lose some fields. More details on this post. #68851797 The only good choice is using mongodump / mongorestore. It's not hard to use itMilli
U
5

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

Unexceptionable answered 8/1, 2021 at 15:37 Comment(2)
Question talks about copying databases not copying all collections. To export data mongo dump should be used.Meshach
Compass is not good for this usage. To use compass you need to import every collection to csv/json. But it might break/lose data, you will need to it manually for every collection, and the indexes won't be exported.Milli
N
3

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>
Northerner answered 3/1 at 17:25 Comment(1)
This is the right answer. There is not a good way using mongodb compass.Milli

© 2022 - 2024 — McMap. All rights reserved.