MongoDB / Pymongo request with date

I am trying to get the data that I inserted into mongodb through pymongo.

My embed code below (after parsing through 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))}) 

Then I introduced two data points into the shell.

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

Then I tried to query the data range between these two data points and got a return.

 >>> 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> 

The 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 request to return everything between the "start" and "end" data?

Also, how can I get this in an understandable way?

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

Thanks.

+7
source share
2 answers

Repetition of existing basic 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 a different place cursor object?

Where should it be?

You see two different instances of the cursor that are likely to return the same result set - or?

+15
source
 @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}})) 
0
source

Source: https://habr.com/ru/post/922863/


All Articles