Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB / Pymongo Query with Datetime

I am trying to retrieve the data I have inserted into mongodb via pymongo.

My code for insert is below (after parsing via regex)

if connection is not None:
    db.model.insert({"time": datetime.datetime(int(int3), int(int1),
                                               int(int2), int(int4),
                                               int(int5), int(int6),
                                               int(int7))})

I then entered two data points in the shell.

>>> start = datetime.datetime(2012, 2, 2, 6, 35, 6, 764)
>>> end = datetime.datetime(2012, 2, 2, 6, 55, 3, 381)

I then tried to query the range of data between these two data points and received what is returned.

>>> db.wing_model.find({'time': {'$gte': start, '$lt': end}})
<pymongo.cursor.Cursor object at 0x0301CFD0>
>>> db.wing_model.find({'time': {'$gte': start, '$lt': end}})
<pymongo.cursor.Cursor object at 0x0301C110>

Data is listed below.

[02/02/2012 06:32:07.334][INFO]
[02/02/2012 06:32:07.334][INFO]
[02/02/2012 06:32:07.334][INFO]
[02/02/2012 06:32:13.711][INFO]
[02/02/2012 06:32:13.711][INFO]
[02/02/2012 06:32:13.711][INFO]
[02/02/2012 06:32:22.473][INFO]
[02/02/2012 06:32:22.473][INFO]
[02/02/2012 06:32:22.473][INFO]
[02/02/2012 06:35:06.764][INFO]
[02/02/2012 06:35:06.765][INFO]
[02/02/2012 06:35:06.765][INFO]
[02/02/2012 06:54:52.008][INFO]
[02/02/2012 06:54:52.008][INFO]
[02/02/2012 06:54:52.008][INFO]
[02/02/2012 06:54:59.512][INFO]
[02/02/2012 06:54:59.512][INFO]
[02/02/2012 06:54:59.512][INFO]
[02/02/2012 06:55:03.381][INFO]
[02/02/2012 06:55:03.381][INFO]
[02/02/2012 06:55:03.381][INFO]
[02/02/2012 06:55:06.142][INFO]
[02/02/2012 06:55:06.142][INFO]
[02/02/2012 06:55:06.142][INFO]
[02/02/2012 06:55:09.652][INFO]
[02/02/2012 06:55:09.652][INFO]
[02/02/2012 06:55:09.652][INFO]
[02/02/2012 06:55:13.396][INFO]
[02/02/2012 06:55:13.396][INFO]

How do I get my query to return everything between the 'start' and 'end' data?

Also, how do I receive this in an intelligible form?

Finally, why does the same query return different cursor object locations?

Thank you.

like image 877
user1590999 Avatar asked Aug 14 '12 17:08

user1590999


1 Answers

Repeating existing basic tutorial documentation:

start = datetime.datetime(2012, 2, 2, 6, 35, 6, 764)
end = datetime.datetime(2012, 2, 2, 6, 55, 3, 381)

for doc in db.wing_model.find({'time': {'$gte': start, '$lt': end}}):
    print doc

Finally, why does the same query return different cursor object locations?

Where should that be the case?

You see two different cursor instances which will likely return the same result set - or?

like image 168
Andreas Jung Avatar answered Sep 20 '22 12:09

Andreas Jung