5

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.

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It looks like I overlooked --fields option. Now everything is working well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.