Reputation: 185
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.
Upvotes: 15
Views: 49096
Reputation: 392
@staticmethod
def _get_results_to_json(data):
"""Get documents from a MongoDB search result.
Transforms MongoDB BSON documents into JSON serializable documents.
This process converts the ObjectIds into hexadecimal strings.
Parameters
----------
data : `~pymongo.cursor.Cursor`
A MongoDB search result.
Returns
-------
|list| of |dict|
A list of JSON serializable documents.
"""
if isinstance(data, Cursor):
data = list(data)
if isinstance(data, list):
for doc in data:
doc['_id'] = str(doc['_id'])
elif isinstance(data, dict):
data['_id'] = str(data['_id'])
return data
_get_results_to_json(db.wing_model.find({'time': {'$gte': start, '$lt': end}}))
Upvotes: -2
Reputation:
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?
Upvotes: 31