PrezOfTheUnitedIdiots
PrezOfTheUnitedIdiots

Reputation: 181

How to transfer a database from MongoDB Compass to MongoDB Atlas

I have an existing database for a discord bot in MongoDB Compass v1.28.1 I want to transfer all the data in the database to mongodb atlas because of its more extensive functionality and to not have to wait for compass to take ages to load each time I open it. However when I follow the steps to connect that are provided in Atlas, the pop-up that's supposed to appear when I copy a path to the clipboard doesn't appear, and nothing happens. I tried to connect through my app in VSCode, the same way I did for Compass, using mongoose. Still no collections are loading or any data being stored. I have made my schemas etc. which work perfectly fine in Compass...

Upvotes: 6

Views: 13326

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

Migration to Atlas is documented at https://docs.atlas.mongodb.com/import/

To save you some reads, you have to options - export/import and mongodump/mongorestore.

I would recommend to try export/import first. It's built into Compass https://docs.mongodb.com/compass/current/import-export/ and must be simpler to use considering limited experience with mongo. It's UI oriented so just follow the click-through guide in the documentation.

Unfortunately it has some limitations related to data type conversion from BSON to JSON and may be a bit tedious if you have large number of collections.

In this case you will need to follow CLI mongodump/mongorestore way @barrypicker suggested in the comments. Both commands are available in cmd and PowerShell consoles.

First you backup your local database https://docs.mongodb.com/v4.2/reference/program/mongodump/:

mongodump --uri="mongodb://username:password@localhost:27017/discordbot"

username and password are the ones you use in compass to connect to the source database.

It will create dump directory with all collections you have.

Then you have to upload the backup to Atlas:

mongorestore --uri="mongodb+srv://username:[email protected]/database" dump/

username and password are the ones you use to connect to atlas cluster, listed in the "Security/Database Access" section.

You can get the exact subdomains for the --uri part from Atlas. In the dashboard click "Connect" button for the cluster you want to connect to, then choose "shell" as the connection method in the connection pop-up:

enter image description here

Upvotes: 10

Related Questions