Search code examples
mongodbcsvmongoexport

MongoDB: Issue when using mongoexport with --query option


When I try to take backup with mongoexport using the --query option to get the documents whose status is equal to A, facing the below error:

mongoexport --port 27017 --db ex --collection A --type=csv --fields _id,status --query '{"status":"A"}' -o eg.csv

error validating settings: query ''{status:A}'' is not valid JSON

Please let me know how to use --query option.


Solution

  • Assuming you run this from the DOS command prompt, you need to swap the single and double quotes. You need to wrap the entire query in double quotes and use single quotes inside the JSON document like this:

    --query "{'status':'A'}"
    

    I have tested this with mongoexport version 3.0.0 and 3.2.0 and it works for both versions.