英文:
pymongo and python: issue with find document using date filter
问题
我正在尝试使用 pymongo 和 Python 从 MongoDB 集合中检索文档。
如果我在 MongoDB Compass 中使用以下筛选条件:
{ "recordTimestamp": { "$gte": '2023-01-24', "$lte": '2023-03-01' } }
我会得到以下预期结果:
{
"fees": "4.00",
"trigger": "immediate",
"price": "12.30000000",
"recordTimestamp": "2023-02-10T12:03:27.197000Z"
}
然而,当我在 pymongo 中使用相同的筛选条件时,我什么都得不到。
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)
我做错了什么?
顺便说一下,我尝试了以下替代方法,它也可以得到预期的结果,但不使用 pymongo 的 find 选项。
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)
英文:
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)
答案1
得分: 0
理想情况下,您应该将日期存储为BSON日期类型。如果您只能使用字符串,那么您可以这样做:
stdateval = '2023-01-24'
endateval = '2023-03-01'
dtFilter = { "recordTimestamp": {"$gte": stdateval, "$lt": endateval}}
英文:
Ideally you should store your dates as a BSON date types. If you are stuck with strings then you can do:
stdateval = '2023-01-24'
endateval = '2023-03-01'
dtFilter = { "recordTimestamp" : {"$gte": stdateval,"$lt": endateval}}
答案2
得分: 0
分享我找到的解决方案。
使用过滤器解决了。
{
"$or" : [
{ 'recordTimestamp': {$gte: '2023-01-24'} },
{ 'recordTimestamp': {$lte: '2023-03-01'} }
]
}
英文:
Sharing solution I found.
resolved using filter
{
"$or" : [
{ 'recordTimestamp': {$gte: '2023-01-24'} },
{ 'recordTimestamp': {$lte: '2023-03-01'} }
] }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论