Emanuele
Emanuele

Reputation: 2394

MongoDB: How to properly export a collection from MongoDB to my computer?

Given the following database and related collection scows.tasks, how do I export 2GB it for storage purposes so to make space?

storage

I have been trying to do it using various ways but none of them worked:

mongoexport --verbose --db scows --collection tasks --out tasks.json --verbose

mongoexport -vvvv --db scows --username='my_username' --password='my_passwd' --collection tasks --out /home/to/Desktop/storageMongo/tasks.json -f 'my_passwd'

mongoexport --host=127.0.0.1 --db scows --collection tasks --out tasts.json

And in doing this I always got the same exact error:

2020-02-13T12:55:17.183-0500 will listen for SIGTERM, SIGINT, and SIGKILL 2020-02-13T12:55:20.690-0500 error connecting to db server: no reachable servers

Posts I used to solve the problem but that unfortunately didn't help me out:

1) I used this one but nothing happened

2) This also but same results as above

3) This source was useful but didn't help me find out the problem

4) And lastly this one to help me with the command but I always had the same error.

5) Also as mentioned in the official documentation I am using my Ubuntu 18.04 shell as terminal to input the command instead of the mongo shell.

How do I properly export a collection from MongoDB to my computer?

Upvotes: 4

Views: 7333

Answers (1)

Valijon
Valijon

Reputation: 13113

Try one of suggested option (Ubuntu terminal):

#localhost:27017 security disabled
mongoexport --db scows --collection tasks --out /tmp/tasks.json

#some_ip:some_port security disabled
mongoexport --host="some_ip:some_port" --db scows --collection tasks --out /tmp/tasks.json

#some_ip:some_port security enabled
mongoexport --host="some_ip:some_port" --username=user --password=pass --db scows --collection tasks --out /tmp/tasks.json

This will export JSON file (no compression) into /tmp directory

But if you export with mongodump command, you can compress your exported data

mongodump --host="some_ip:some_port" --username=user --password=pass --db scows --collection tasks --gzip --out /tmp

This will export BSON structured files (compressed) into /tmp/scows directory

EDIT: Export from MongoDB Atlas, use this:

mongoexport --uri="mongodb+srv://username:[email protected]/scows" --collection tasks --out /tmp/tasks.json

2020-02-13T20:20:51.387+0100    connected to: mongodb+srv://[**REDACTED**]@vessel-tracker-cluster-x2lpw.mongodb.net/scows
2020-02-13T20:20:52.522+0100    [........................]  scows.tasks  0/XXX  (0.0%)
2020-02-13T20:20:52.642+0100    [########################]  scows.tasks  XXX/XXX  (100.0%)
2020-02-13T20:20:52.643+0100    exported XXX records

EDIT 2: User has DNS issue which ignores --uri parameter and connects to localhost. Adding public DNS to resolve.conf, mongoexport was able to export the data

Upvotes: 6

Related Questions