'Migrating to MongoDb Atlas from Local

I'm trying to migrate my local MongoDB over to Atlas.

I managed to run the mongodump command with localhost and can see the files in ./dump/data.bson

However, when I now want to restore this dump onto Atlas, I get "Failed: error connecting to db server: no reachable servers".

This is odd because I can connect to Atlas from the mongo shell (v4.0) using this command: "mongo mongodb+srv://cluster0-xxxxxx.mongodb.net/test --username Bob" where i get prompted for password and connect fine.

This is the mongorestore command that's giving me the above connection error:

mongorestore --ssl --host mongodb+srv://cluster0-xxxxx.mongodb.net/test --username Bob --authenticationDatabase admin --dir dump/data --password Test123

Appreciate your help.



Solution 1:[1]

Ok so I found the solution.

You need to export the collection as JSON (can do so via Compass -> Collection (dropdown menu) -> Export Collection).

Then you need to use mongoimport (as opposed to mongorestore). Full info here: https://docs.atlas.mongodb.com/import/mongoimport/

I guess mongorestore didn't work because my local is a standalone DB whereas Atlas is a replica set... although I haven't confirmed this.

EDIT I tried again recently and got confused. So I'm spelling it out fully.

In the mongodb directory, you need to run mongoimport --uri "connectionURI" --collection "collectionName" --file "filename.json"

The connection URI you get with the option called "connect your application" in atlas, it should include your password (you have to escape special characters).

Hope it helps.

Solution 2:[2]

There is clear documentation for this on the atlas site. When you look at your cluster there will be a tab for Cmd Line Tools.

Here are the steps.

  1. Run mongodump on the local system with no options.

  2. From the same system run: mongorestore --uri mongodb+srv://<USER>:<PASSWORD>@thename.mongo.gives.your.cluster.mongodb.net

You may need to open the atlas firewall to allow connections from the IP of the local machine if you haven't already.

Solution 3:[3]

try changing the beginning of the connection string from mongodb+srv:// to mongodb://

Solution 4:[4]

IF you need upload a .BSON you need use mongo restore, of this way

enter image description here

you need write de command on before the path dump/ for example if you system folds is

example/example/mydata => hi.bson bye.bson you should create "dump" file

like this example/example/dump/mydata => hi.bson bye.bson

and run the command on the example/example/, because mongo will search "/dump"

also

you need specificate why is the BD with the flag --db in the end of de command

Solution 5:[5]

Use

mongorestore --ssl --host <host> --authenticationDatabase admin --dir="<dumpDirectory>" -u <adminUserName> --password <password>

<host>: get from mongo atlas dashboard (click on the cluster name) ex: cluster0-shard-00-00-cbei2.mongodb.net:27017 
<dumpDirectory>: The folder where database is dumped using mongodump
<adminUserName>: admin User for that database
<password>: admin associated password

Solution 6:[6]

You can easy import/export like this:

mongoimport --uri mongodb+srv://<USERNAME>:<PASSWORD>@your-cluster.mongodb.net/<DATABASE_NAME> --collection <COLLECTION> --type <FILETYPE> --file <FILENAME>

mongoexport --uri mongodb+srv://<USERNAME>:<PASSWORD>@your-cluster.mongodb.net/<DATABASE_NAME> --collection <COLLECTION> --type <FILETYPE> --out <FILENAME>

You also can found these on Your Cluster > Cmd Line Tools

Solution 7:[7]

=>Windows

  1. Run mongodump on the local system with no options.

  2. From the same system run:

     mongorestore --uri mongodb+srv://<Yorname>:<*******>@cluster0.dvxc9.mongodb.net/Databasename --username <Yourname> -d Databasename 'C:\Users\user\dump\Databasename' -v
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ugbun
Solution 3 scottc11
Solution 4 Sebastian Medina
Solution 5 gurula
Solution 6 trungquandev
Solution 7 Suraj Rao