Search code examples
mongodbmongoexport

mongoexport not working if you add query of selection


If you want to export any collections of any of your mongodb database, you can use mongoexport command. For example:

mongoexport --db dbname --collection collectionName --query '{"fields":1}' --out output.json

However, if you add any of selection criteria to the query, mongoexport command doesn't work. For example, if you run the following command:

mongoexport --db dbname --collection collectionName --query '{},{"fields":0}' --out output.json

the resultant JSON file includes data of every field, despite me excluding a filed named fields.

So why does this such strange behavior occur? And how can I fix it?

For your information, db.colName.find({},{"fields":0}) in mongoDB shell works as usual.

I'm on MongoDB 2.4.3 and OS X 10.9.

Thanks.


Solution

  • That's because --query argument takes only query and there is no option to pass projection. See mongoexport --query in official documentation. If you want to use a projection you have to add --fields option