Search code examples
mongodbmongoexport

Mongoexport error parsing query


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'}"


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'}"