开发者

How do I dump data for a given date

I am using mongoexport for exporting data for a given dates..my command is this

mongoexport -d project -c coll  --csv -o result -f source -q '{"date":{"$gte":"new Date(2011,1,25)","lt":"new Date(201开发者_JAVA技巧1,2,26)"}}'

but it is not exporting result...i have data stored for 25-feb-2011 and 26-feb-2011 in database..how do I query for dates?


I had the same problem.. I fixed it by using the epoch time..

You can check here http://www.epochconverter.com/

I was using java .. so i used

Date x = new Date (year, month, day)

and got the epoch using x.getTime() and sent it to mongoexport

so ur mongo export statement would be

mongoexport -d project -c coll --csv -o result -f source -q '{"date":{$gte:(Date(1295913600)),$lt:(Date(1298678400)}}'


I think the problem is the way you are specifying the dates.

Firstly, I don't think new Date(....) should be in quotes (would be treated as a string literal not a date?).

Secondly, it looks like new Date(Y,M,D) cannot be used here to specify a date - that gives me the following error:

Assertion: 10340:Failure parsing JSON string near...

I think instead you need to speciy the date numerically.
e.g.
new Date(1234567890)

So the whole thing would be something like:

mongoexport -d project -c coll  --csv -o result -f source -q '{"date":{"$gte":new Date(x)","$lt":new Date(y)}}'

where x = the numerical representation of your start date and y = the end date. Also, added the missing "$" for the "lt" as already pointed out.

Update:
Re: how to find the numerical representation. Had to play around a bit in the mongo shell as relatively new to this myself. here's an example of what I did the mongo shell to get the number (e.g. for 25-feb:

> new Date(2009,1,25) * 1

which gives:

1235520000000

In the shell again, if you then do:

> new Date(1235520000000)

It will confirm it is the correct date:

ISODate("2009-02-25T00:00:00Z")


Unfortunately Wael's query above did not work for me with mongoexport version 2.2.4 but this one did:

$and:[{date:{$gte:new Date(1372801816000)}},{date:{$lt:new Date(1373061846000)}}]}

you'd have to replace the above two epoch numbers with your own. so the mongo export command looks something like (I used --jsonArray instead of --csv)

mongoexport -h your_host:port -d your_db -c your_collection --jsonArray -o output.js -q '{$and:[{date:{$gte:new Date(time_stamp_1)}},{date:{$lt:new Date(time_stamp_2)}}]}'


I just came across this and it seems the problem in the original post is that new Date(2011,2,26) shouldn't have been wrapped in quotes and "lt" should have been "$lt". Also, new Date(2011,2,26) is March 26, 2011 because javascript uses a 0-11 range for month values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜