Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 680
Blaszard Avatar asked Nov 05 '13 22:11

Blaszard


People also ask

What is the difference between Mongodump and Mongoexport?

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. mongodump is a utility for creating a binary export of the contents of a database.

How do I export MongoDB query results to CSV?

Export MongoDB to CSV (e.g. Excel) Open the Export Wizard and select your export source. This screen only appears if you haven't chosen an item in the Connection Tree, run a previous query, or selected specific documents. Next, choose CSV as the export format then click Next.

Where does Mongoexport export to?

The mongoexport tool exports data in Amazon DocumentDB to JSON, CSV, or TSV file formats.


1 Answers

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

like image 181
zero323 Avatar answered Oct 05 '22 23:10

zero323