mongodbmongoexport

Query for Mongoexport


I want to export a collection projection according to a query, the syntax I am using to execute mongoexport is:

./mongoexport -d myDB -c myCollection -q "myQuery" -o output.json

I have tried to use this filter in mongo shell, but I can't format it correctly, so it is legal to stand for "myQuery":

{
  'run.session.game._id':'gameId1',
  'run.session.device._id':{$in:['value1','value2']},
  'createdAt':{
      $gte:ISODate('2016-06-05T15:14:22.163Z'),
      $lte:ISODate('2017-06-05T15:14:22.163Z')
   }
}

It works if I only have the first filter, but as soon as I put in line 2 for device id, I get an error. The error message looks like this:

2016-10-07T22:54:02.333+0200    
error validating settings: query '[123 39 114 117 110 46 115 101 115 
115 105 111 110 46 103 97 109 101 46 95 105 100 39 32 58 32 39 90 71 
120 88 109 104 100 66 69 83 39 44 39 114 117 110 46 115 101 115 115 105 
111 110 46 100 101 118 105 99 101 46 95 105 100 39 58 123 58 91 79 98 
106 101 99 116 73 100 40 39 101 104 55 73 77 117 112 107 52 112 39 41 
93 125 125]' is not valid JSON: invalid character ':' looking for 
beginning of object key string 
2016-10-07T22:54:02.333+0200 try 'mongoexport --help' for more
information

Please help :( I know there is something about the query has to be in strict JSON format, but I don't really know how to convert it...


Solution

  • From mongoexport documentation:

    You must enclose the query in single quotes (e.g. ') to ensure that it does not interact with your shell environment.

    So try switching your "'s and ''s this way:

    ./mongoexport -d myDB -c myCollection -q '{
      "run.session.game._id":"gameId1",
      "run.session.device._id":{$in:["value1","value2"]},
      "createdAt":{
          $gte:ISODate("2016-06-05T15:14:22.163Z"),
          $lte:ISODate("2017-06-05T15:14:22.163Z")
       }
    }' -o output.json