3
mongoexport --db ucc_prod /host:myserver /port:27017 --username user1 --password password1 /query:'{copysheet: {$regex: "/^.*pdf/"}}' /out:copysheets.csv --type=csv --fields svOrderId,svItemId --collection copies

gives me error

2016-09-02T08:17:34.632-0500    error parsing command line options: unknown option "^.*pdf/}}'"

What syntax am I missing here?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Prabhat
  • 4,066
  • 4
  • 34
  • 41

1 Answers1

7

You may use

--query "{ 'copysheet': { '$regex': '^.*pdf', '$options':'' }}"

The point is that you should pass the data to the query argument as JSON.

See reference:

--query <JSON>, -q <JSON>
Provides a JSON document as a query that optionally limits the documents returned in the export. Specify JSON in strict format.

Note: on different systems, you might need to swap single with double quotes.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 3
    This answer is great! Just a bit more hint: In my case, mongoexport in Windows CMD forced me to wrap the query string with double quotes, then the remote Mongo server required me to wrap field names and strings with double quotes too. Eventually I had to escape the double quotes in the query in order for it to work. E.g., `--query "{\"fieldName\": {\"$regex\": \"search-string\", \"$options\": \"i\"}}"`. – Marty ZHANG Sep 15 '20 at 08:30