Mongoexport error parsing query

做~自己de王妃 提交于 2019-12-23 11:47:01

问题


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:


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



回答2:


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


来源:https://stackoverflow.com/questions/21456714/mongoexport-error-parsing-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!