Below is the document structure of my collection in MongoDB.
Using pymongo I want to filter between the documents using date range on creation_date key which is inside record_status key.
Here is the structure of the document:
{
"_id" : ObjectId("5b8a199cf4a98b075490bb47"),
"Pls_Customer_Id" : "C1810151",
"FirstName" : "SCHNEIDER ELECTRIC INDIA PVT LTD",
"LastName" : "SCHNEIDER ELECTRIC INDIA PVT LTD",
"Address1" : "DELHI",
"State" : "DELHI",
"MobileNumber" : "989898989898",
"DeletedFlag" : false,
"Country" : "India",
"record_status" : {
"created_user" : "uploadBatchProgram",
"creation_date" : ISODate("2018-09-01T04:46:20.055Z")
},
"PoliciesRef" : [
ObjectId("5b8a199cf4a98b075490bb45")
],
"__v" : 0,
"customerType" : "Regular"
}
If I understand correctly, you want to filter documents between from
and to
date ranges of record_status.creation_date
.
Below is the code for the filter which you require.
from pymongo import MongoClient
from datetime import datetime
from_date_range = datetime(2020, 1, 1) # Format -> (yyyy, mm, dd)
to_date_range = datetime(2020, 5, 20) # Format -> (yyyy, mm, dd)
collection = MongoClient()["<DB-NAME>"]["<COLLECTION-NAME>"]
for cursor in collection.find({
"record_status.creation_date": {
"$gte": from_date_range,
"$lt": to_date_range
}
}):
print(cursor)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.