3

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?

Emanuele
  • 2,194
  • 6
  • 32
  • 71

1 Answers1

4

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:password@vessel-tracker-cluster-x2lpw.mongodb.net/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

Valijon
  • 12,667
  • 4
  • 34
  • 67
  • Thanks for reading the question, I still have the same connection error. I wonder if `--host="some_ip:some:port"` needs to receive this string: `mongodb+srv://username:password@vessel-tracker-cluster-x2lpw.mongodb.net/trackingtest?retryWrites=true&w=majority` – Emanuele Feb 13 '20 at 19:11
  • Ok something is happening: it is connecting to `localhost` but I get this error: `2020-02-13T14:37:37.513-0500 connected to: localhost` `2020-02-13T14:37:37.530-0500 Failed: Failed to parse: { find: "tasks", filter: {}, sort: {}, skip: 0, snapshot: true, $db: "scows" }. Unrecognized field 'snapshot'.` – Emanuele Feb 13 '20 at 19:38
  • But I don't know where 'snapshot' is coming from. It is not a field I introduced in the db. I am sure of that. – Emanuele Feb 13 '20 at 19:39
  • Let's continue on [chat.stackoverflow.com](https://chat.stackoverflow.com/rooms/207793/discussion-between-valijon-and-emanuele) – Valijon Feb 13 '20 at 19:55
  • @emanuele updated my answer with conclusions. Open new issue to ask community how to solve DNS problem for your Ubuntu 18.04 – Valijon Feb 14 '20 at 09:13
  • I will mark your answer as correct because it worked at the end. And meanwhile I am going to post a ticket on the DNS! Thanks for your help! :) – Emanuele Feb 14 '20 at 12:09
  • hi Valijon, how are you? I just wanted to let you know that I opened ticket [here](https://stackoverflow.com/questions/60339320/mongodb-dns-issue-of-resolv-conf-connecting-to-mongodb) about the DNS issue of last week. Sorry it took me a little long but I wanted to make sure I described the question well, with enough details and with the proper print screen. It was a lot of work. If you could please take a look and tell me if it is clear I would really appreciate that. If not I will edit it. Thanks :) – Emanuele Feb 21 '20 at 13:12
  • Hi. How are you doing? I've updated a bit your question, I hope someone can help you setup correctly DNS. Good luck – Valijon Feb 21 '20 at 15:53
  • Hi :) I am doing good and what about you? Thanks for the edits. In the meantime I will just export data the way we solved it last week until someone can provide a DNS fix solution. Have a good weekend! – Emanuele Feb 21 '20 at 16:02