27

I have remote database in Atlas with name of "test" and I want to download collection name image_table as a JSON file.

In mac terminal:

$ mongoexport –db test –collection image_table image.json I got the error>

020-01-16T13:49:12.822+0100 error parsing command line options: too many positional arguments: [–db test –collection image_table image.json] 2020-01-16T13:49:12.822+0100 try 'mongoexport --help' for more information

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Farbod Aprin
  • 990
  • 1
  • 10
  • 20

5 Answers5

17

MongoDB Compass has many flaws and may fail you even with such a trivial task as exporting a collection. E.g. it will skip some fields in the export. The app itself says: "The fields displayed are from a sample of documents in the collection...". From my experience, a sample Compass uses is very small. I lost data even when exporting 100 documents or so.

Using mongoexport is easy. If you use mongo 3.6+, this will work:

mongoexport --uri mongodb+srv://<username>@atlas-cluster-url.mongodb.net/<db-name> --collection <collection-name> --out <path-to-export>

13

I could find a bit more straightforward answer in Mongo Compass :

enter image description here

Just install mongo compass connect to your atlas remote DB: get the hostname like "cluster0-shard-00-00-rcapo.mongodb.net XXXXX" from your remote altas cluster then connect to the database.

then you can download each document as JSON or CSV format. thanks, mongo DB compass developers team.

Farbod Aprin
  • 990
  • 1
  • 10
  • 20
3

I had this same problem. In my case, I was using mongoexport with the --query option, which expects a JSON document, such as:

mongoexport ... --query {field: 'value'} ...

I needed to surround the document with quotes:

mongoexport ... --query "{field: 'value'}" ...

Arpit Jain
  • 1,217
  • 8
  • 27
2

I wanted to export all the collections from my atlas to local database. After facing a lot of issues with mongodb compass and mongoshell I ended up using NOSQL booster for mongodb. Downloading Link

It exported all the collections at once with ease and then I was able to import them into my local mongodb database using the same tool.

Sanan Ali
  • 2,349
  • 1
  • 24
  • 34
0

This might be the Required Solution:

mongodump --uri="mongodb://username:password@host:port/dbname" --out=/path/to/backup/directory

This will dump all of your collections to /directory