Mongoexport returning 0 records

Hello! I am trying to export a large collection named ‘questions’ from a database named ‘sc_data’ on my localhost. I have created a json file named questions.json in a folder named db_transfer on my desktop for storage. I have the latest version of mongodb_database_tools and mongodb-community version 5.0.4 installed on my machine.

I am running this command in my terminal:
mongoexport --collection questions --db sc_data --out /Users/bonnieowens/desktop/db_transfer/questions.json

and getting this response:
connected to: mongodb://localhost/
exported 0 records

I can see in Compass that I have that db in localhost and I have that collection in the db, and it is not empty.

I have tried specifying the port (–port 27017) and using the syntax in the docs (–db=sc_data). I am sure that it’s a simple error, but I can’t find it.

Thanks for your help!

Why create questions.json?
You have to give just the name of your output file.
Please show full log

Just to have it go to that very specific place. I have also tried the command without a specified --out value, but it still doesn’t work.


Can you repeat that to see if that works for you?

polux@hafx:/tmp$ cat questions.json 
polux@hafx:/tmp$ mongoimport --db test --collection test --file questions.json 
2022-03-02T17:21:18.882+0100	connected to: mongodb://localhost/
2022-03-02T17:21:18.932+0100	1 document(s) imported successfully. 0 document(s) failed to import.
polux@hafx:/tmp$ mongoexport --db test --collection test --out test.json 
2022-03-02T17:21:24.732+0100	connected to: mongodb://localhost/
2022-03-02T17:21:24.736+0100	exported 1 record
polux@hafx:/tmp$ cat test.json 


That works! Any idea why my other cmd is not working? Thanks!

collection and db not in the right order? Pointing to the wrong collection? It must be something really silly :smiley: !

I’m not entirely sure why, but restarting my computer and then restarting mongodb-community that I had running in the background fixed this issue. I thought I would share in case someone else experiences the same issue.


Happy it’s fixed now :smiley: !


1 Like