I am trying to retrive document from mongodb using pymongo and python from these mongodb collection.
{
"fees": "0.00",
"trigger": "immediate",
"price": "18.30000000",
"recordTimestamp": "2023-01-10T12:03:27.197000Z"
},
{
"fees": "4.00",
"trigger": "immediate",
"price": "12.30000000",
"recordTimestamp": "2023-02-10T12:03:27.197000Z"
},
{
"fees": "1.00",
"trigger": "immediate",
"price": "10.30000000",
"recordTimestamp": "2023-03-10T12:03:27.197000Z"
}
If I use mongoDB compass and do this filter
{recordTimestamp: {$gte: '2023-01-24',$lte: '2023-03-01'}}
I get below expected result.
{
"fees": "4.00",
"trigger": "immediate",
"price": "12.30000000",
"recordTimestamp": "2023-02-10T12:03:27.197000Z"
}
however when I use same filter in pymongo I get nothing.
stdateval = datetime(2023,1,24)
endateval = datetime(2023,3,1)
dtFilter = { "recordTimestamp" : {"$gte": stdateval,"$lt": endateval}}
collection = DBModule.database[collectionName]
jsnObj_list = collection.find(dtFilter)
What am I doing wrong?
btw I tried this alternative and it also gets the expected result. but not with pymongo find option.
stdateval = datetime(2023, 1, 24)
endateval = datetime(2023, 3, 1)
jsnObj_list = collection.find({})
filtered_list = []
for item in jsnObj_list:
recordTimestamp = datetime.strptime(item['recordTimestamp'], "%Y-%m-%dT%H:%M:%S.%fZ")
if stdateval <= recordTimestamp < endateval:
filtered_list.append(item)
Sharing solution I found.
resolved using filter
{
"$or" : [
{ 'recordTimestamp': {$gte: '2023-01-24'} },
{ 'recordTimestamp': {$lte: '2023-03-01'} }
] }