Search code examples
mongodbmongoexport

Why does mongoexport query with $exist fail?


I am trying to mongoexport (Version 2.6) MongoDB data into csv format using the command as follows

mongoexport --port 27017 -d test -q "{userId:{$exists:true} , name:'John'}"-c user_datas -f userId --csv -o /myOutFile.csv

and i got this error message:

assertion: 16619 code FailedToParse: FailedToParse: First character in field must be [A-Za-z$_]: offset:9 of:{userId:{true},name:John}

according to error message that something happened on '$exists' that caused the error . whats wrong with my command?


Solution

  • You need to invert the quotes:

    '{userId: {$exists: true} , name: "John"}'
    

    Working command:

    mongoexport \
      --port 27017 \
      -d test \
      -q '{userId: {$exists: true} , name: "John"}' \
      -c user_datas \
      -f userId \
      --csv \
      -o /myOutFile.csv