Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoexport -q ISODate query

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)}}"
like image 500
user_s Avatar asked Dec 21 '14 16:12

user_s


4 Answers

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" } }}'
like image 117
toddg Avatar answered Nov 03 '22 19:11

toddg


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' } } }"
like image 20
wdberkeley Avatar answered Nov 03 '22 19:11

wdberkeley


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}}"
like image 1
Alan Deivson Avatar answered Nov 03 '22 19:11

Alan Deivson


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.

like image 1
Abrham Smith Avatar answered Nov 03 '22 21:11

Abrham Smith