6

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

1
  • What if I need to add spaces e.g. in creating date: new Date()? Commented Jul 27, 2018 at 10:16

2 Answers 2

5

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'}"
Sign up to request clarification or add additional context in comments.

2 Comments

The line you provided works as I've described (I tried it). I'd imagine you're trying on a more complex command-line?
( the first time I tried it I had a space in it e.g. "{'recent': 'yes'}" )
0

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

Comments

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.