Question

Im trying to do a mongoexport to CSV but only selecting certain records with a query. Here's my command (windows 7 cmd):

mongoexport --host foo.com --port 27017 --username bar -p --db foo --csv --fields col1,col2,col3 --collection bar --out dump_q.csv --query '{"recent":"yes"}'

However after entering the password, I get an error:

assertion: 16619 code FailedToParse: FailedToParse: Expecting '{': offset:0

The command works fine without the query argument but I cant figure out whats wrong with the query:

--query '{"recent":"yes"}'

Any help much appreciated


Summary of answer:

  1. Make sure you use double quotes on enclose the query and single quotes to enclose strings e.g.

    --query "{'recent':'yes'}"

  2. Also make sure you don't have a space in your query otherwise the command prompt will parse it as another argument. So don't have:

    --query "{'recent': 'yes'}" (notice the space in-between)

  3. Queries which include nested fields don't work such as:

    --query "{'folder.recent':'yes'}"

Was it helpful?

Solution

You'll need to use double quotes to contain the query string (and either single quotes or two quotes to escape inside of the string)

--query "{'recent':'yes'}"

Complete:

mongoexport --host foo.com --port 27017 --username bar -p
        --db foo --csv --fields col1,col2,col3 
        --collection bar --out dump_q.csv --query "{'recent':'yes'}"

OTHER TIPS

From mongoexport documentation:

--query , -q

Provides a JSON document as a query that optionally limits the documents returned in the export.

Your query string seems to be correctly formated. You can even ommit the double quotes around recent.

Single or double quotes don't seem to matter, as long as you are persistent in using different types on the outside and the inside.

Are you sure this is a valid query though? What is the output if you run the following in the database? What about a find()?

db.bar.count({"recent":"yes"})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top