How to transfer a database from MongoDB Compass to MongoDB Atlas
Asked Answered
P

1

6

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

Peaceful answered 19/8, 2021 at 16:11 Comment(10)
MongoDB Compass is not a database and does not hold any data. It is a GUI tool to view data in a MongoDB database (this MongoDB database could be installed on your localhost or on a network resource). Once you identify where your source database is, try to identify the version, and also identify the target MongoDB Atlas cluster version as well and post results here please.Osher
How can I see where the source is and find the version.Peaceful
it is based on your MongoDB Compass connection string. I would start by looking at the connection string and identifying the host name and port. The database version can be seen in Compass.Osher
Oh alright thanks found that, there are 2 connection strings: Short one: mongodb://localhost:27017/discordbot Long one: mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false There is a tab saying Edition and it says MongoDB 4.2.15 Community. Hopefully that's the right info.Peaceful
OK great, you have a database on our local box. One one connection string you specify the starting database as discordbot. I assume this is the database you wish to migrate to Atlas? If so, simply use the mongodump tool to backup the local database, then use the mongorestore tool to restore to Atlas. It is fairly trivial once you have done it before, but if you never have, then you will slog through a mirad of security blocks. Firstly, you must have the mongodump and mongo restore tools. Download them if you don't already have them.Osher
You can use any mongodump/mongorestore version 4.2 or newer. The target Atlas cluster can be 4.2 or 4.4, but not 5.0 (because you can only restore to one version newer). If you like, you can restore to Atlas 4.4 then upgrade to 5.0.Osher
If you want 4.2 you can download the entire mongodb community server (which includes mongodump and mongo restore) at mongodb.com/try/download/communityOsher
If you want 4.4 you need to download the tools, not the server, to get mongodump and mongorestore (They repackaged the mongodump and mongorestore in version 4.4 into a package called tools)Osher
I'm guessing that mongorestore and mongodump are mongo shell commands? or can I just use them in my console emulatorPeaceful
Let us continue this discussion in chat.Osher
I
9

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

Incoherence answered 24/8, 2021 at 11:27 Comment(7)
When I run mongodump, the console returns an error saying Failed: can't create session: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed. How can I solve that, and what is SCRAM-SHA-1Peaceful
I assume export/import didn't work, did it?Incoherence
Tried that, the program said 'export' is not recognized as an internal or external command, operable program or batch file. I ran the command in the folder where the mongo server is...Peaceful
Hmmm, did you read the page behind the link? Export is not a command, its a button in compass.Incoherence
I'm using version 1.28.1 on MongoDB Compass and I cannot find it therePeaceful
And I cannot update the version because I am using Windows 8.1 Pro on a shared PCPeaceful
As of today, v1.28.1 is the latest version of compass. Unfortunately Stackoverflow format is not quite suitable to address "I can't find" kind of questions. I would strongly recommend to invest some time to learn MongoDB basics. university.mongodb.com/courses/M001/about is free, it covers export/import functionality and has a Q&A forum dedicated to such questions.Incoherence

© 2022 - 2024 — McMap. All rights reserved.