16

I'm trying to run this:

mongoexport.exe -h *MYHOST* -p *MYPORT* -q "{'time':{'$gte': ISODate('2014-12-21 12:57:00.506Z'),'$lt': ISODate('2014-12-21 12:59:00.506Z')}}"

Or this(the gte & lt without - ' ):

mongoexport.exe -h *MYHOST* -p *MYPORT* -q {'time':{$gte: ISODate('2014-12-21 12:57:00.506Z'),$lt: ISODate('2014-12-21 12:59:00.506Z')}}

The query works fine on Robomongo, But with mongoexport it throws: "too many positional arguments"

I know I can run the following instead, But I don't want to use the date converter everytime I need to execute a query.

mongoexport.exe -h *MYHOST* -p *MYPORT* -q "{'time':{$gte: new Date(14191
66620506),$lt: new Date(1419166740506)}}"
user_s
  • 1,058
  • 2
  • 12
  • 35
  • Duplicate of this newer question, [Use mongoexport with a --query for ISODate](https://stackoverflow.com/questions/36319052/use-mongoexport-with-a-query-for-isodate), which has better answers. – Dan Dascalescu Mar 02 '20 at 19:01

6 Answers6

22

mongoexport queries require the use of strict-mode MongoDB extended JSON. You can read more about that flavor of extended JSON in the MongoDB Manual article on extended JSON. In your specific case, the proper way to write the first query in the mongoexport command is

mongoexport.exe -h *MYHOST* -p *MYPORT* -q "{ 'time' : { '$gte' : { '$date' : '2014-12-21 12:57:00.506Z' },'$lt' : { '$date' : '2014-12-21 12:59:00.506Z' } } }"
wdberkeley
  • 11,531
  • 1
  • 28
  • 23
  • Is this answer still valid? Querying by string instead of ISODate seems not to return any results whereas adding ISODate("") constructor will return results - in the mongo shell directly. In mongoexports, adding ISODate("") (or JS Date object) constructor causes an error. – netpoetica Jun 18 '15 at 12:48
  • 1
    It appears that this will work properly, but the timestamp can be very finicky. Epoch time must be formatted to include milliseconds and iso time (as string like above) *must* contain microseconds. – netpoetica Jun 18 '15 at 13:57
  • Additionally, if you write the timestamp like "2015-10-18T00:00:00Z00:00" then mongoexport will complain about extra text: 00:00. – forgetso Nov 18 '15 at 10:59
  • 7
    On Mac I could not get this to work. I had to use: `mongoexport -d myDb -c myCollection -q '{ "timestamp" : { "$gte" : { "$date" : "2015-11-01T12:57:00.000Z" }}}'` *Note*: the double quotes *had to be* inside the string, with single quotes outside. – Ron Wertlen Jan 08 '16 at 12:25
  • This is not a valid answer for mongo 3.x. – Christian Dechery Jul 12 '18 at 14:43
9

I used @wdberkeley's answer as a starting point but that particular date string did not work for me. I had to use a 'T' to separate the date and the time:

mongoexport --username user --password pass --host host --db dbName --collection coll --type=csv --query '{"_created_at": { "$gte" : { "$date" : "2017-12-21T12:57:00.506Z" } }}'
toddg
  • 2,863
  • 2
  • 18
  • 33
1

Had to use a different syntax in windows.

mongoexport --host 192.168.1.5 --db dbname --collection files --query "{ "ModifyDate" : { "$lte" : { "$date" : '2019-02-17T00:00:00.000Z' } }}"

Difference being the single quotes around the date.

AlexisG
  • 2,476
  • 3
  • 11
  • 25
Abrham Smith
  • 163
  • 1
  • 7
1

I had the same problem with mongo 2.4, you need to use $date with "a 64-bit signed integer for milliseconds" (https://docs.mongodb.com/v2.4/reference/mongodb-extended-json/) in your case:

mongoexport.exe -h *MYHOST* -p *MYPORT* -q "{'time':{'$gte':{"$date": 1419166620506},'$lt': {"$date": 1419166740506}}"
avariant
  • 2,234
  • 5
  • 25
  • 33
0

Update for Late 2021 for Windows:

Consider Git Bash to run it, as I was not able to make it work in Windows Terminal

mongoexport --hostname=$MYHOST --password=$MYPWD --query='{"time":{"$gte":{"$date":"2014-12-21 12:57:00.506Z"},"$lt":{"$date":"2014-12-21 12:59:00.506Z"}}}' mongodb://IP --out=myoutfile.json

Fabien Haddadi
  • 1,814
  • 17
  • 22
0

I have mongoexport version 3.6.1 on Windows. I have to use Git Bash as Fabien Haddadi but in my case works with date in milliseconds

-query '{ "Time": { "$gte": new Date(1609455600000)} }' 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103