15

I have this query but i´m getting a syntax error: unexpected identifier

mongoexport --db ium --collection events \
  --query 'db.events.find({'created_at' : {
      $gte: ISODate("2016-03-01T00:00:00.001Z"),
      $lte: ISODate("2016-03-29T23:59:59:59.000Z")
    }, 
    "name" : "UPDATE_SUCCESS"})' \
 --out guille1_test.json

what can it be wrong?

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
Chanafot
  • 736
  • 4
  • 22
  • 46
  • 1
    try using "created_at" – mugabits Mar 30 '16 at 20:37
  • @jmugz3 There was a bit more wrong than the misuse of "quoting" inside the argument string, as there are clear usage errors to correct as well as something not commonly understood about querying with "dates". – Blakes Seven Mar 31 '16 at 00:51

3 Answers3

25

You need to use "extended json" in queries with mongoexport. So the way to specify "dates" is with $date instead. And the --query is just the "query string" in JSON format. Not the whole command entered into the shell:

mongoexport --db ium --collection events \
  --query '{ 
    "created_at": { 
      "$gte": { "$date": "2016-03-01T00:00:00.001Z" },
      "$lte": { "$date": "2016-03-29T23:59:59.000Z" }
    },
    "name": "UPDATE_SUCCESS"
  }' \
  --out guile1_test.json

Note also the corrected date string in the $lte argument and of course the "quoting" use of '' around the body of the JSON argument and "" around the internal expressions and values. It s important that these types of quotes are different, as well as "shell arguments" should have their "outer" quotes as '', otherwise the "shell" tries to evaluate the expression contained.

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
  • Thank you man, in just a beginner here with mongodb as you can see hehe. Anyway this format also is bringing unexpected error. Perfhaps changing the date format to UNIX epoch? mongoexport --db ium --collection events --query ' {"created_at" : { $gte: { "$date": 2016-03-01T00:00:00.001Z"} }, "name" : "UPDATE_SUCCESS"}' --out guille_test.json – Chanafot Mar 31 '16 at 14:19
  • @Guillermo-Casco The error is because you are still missing quotes around the value to `"$date"`. Here `2016-03-01T00:00:00.001Z"` see the missing `"` before the date. All you need to do is copy/paste what is provided in the answer, since that is the direct translation of what you attempted in your question, but without any format errors in the formed JSON. – Blakes Seven Mar 31 '16 at 22:11
  • Is there a way to use a variable in the argument given to the $date section (e.g., varDate="2016-03-01T00:00:00.001Z")? – Daniel Connelly Oct 03 '20 at 01:50
  • The command works but the output of the file has `"created_at":{"$date":"2021-07-28T00:01:00.766Z"}` How can it be saved in the json format? – developthou Aug 04 '21 at 20:08
3

Another working solution is by using the new Date() constructor as described in the MongoDB manual. This will result in a smaller query body footprint like so:

    mongoexport --db ium --collection events \
  --query '{ 
    "created_at": { 
      "$gte": new Date("2016-03-01T00:00:00.001Z"),
      "$lte": new Date("2016-03-29T23:59:59.000Z")
    },
    "name": "UPDATE_SUCCESS"
  }' \
  --out guile1_test.json

This approach worked for me out-of-the-box while all other alternatives failed. There is a relative article describing this approach here.

chrysanthos
  • 1,388
  • 10
  • 17
  • 4
    With `mongoexport` r4.2.3, I get "Failed: error parsing query as Extended JSON: invalid JSON literal. Position: XX, literal: new". The accepted answer, with `"$gte": { "$date": "..." }`, does work. – Dan Dascalescu Mar 02 '20 at 18:55
1

Best way to achieve it as following. Because new Date and IOSDate would be invalid literal for this command.

For remote host

mongoexport --host {{host}} --username {{username}} --password {{passord}} --authenticationDatabase admin --db {{Database}} --collection {{collection Name}} --query '{ "date" : { "$gt" : {"$date":"2019-10-31T00:00:00.000Z"}  } }'  --type json --out {{path of directory where you would want to export file.}}

For local host

 mongoexport  --db {{Database}} --collection {{collection Name}} --query '{ "date" : { "$gt" : {"$date":"2019-10-31T00:00:00.000Z"}  } }'  --type json --out {{path of directory where you would want to export file.}}