pymongo和Python:使用日期筛选器查找文档时出现问题。

huangapple go评论71阅读模式
英文:

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.

{
  &quot;fees&quot;: &quot;0.00&quot;,
  &quot;trigger&quot;: &quot;immediate&quot;,
  &quot;price&quot;: &quot;18.30000000&quot;,
  &quot;recordTimestamp&quot;: &quot;2023-01-10T12:03:27.197000Z&quot;
},
{
  &quot;fees&quot;: &quot;4.00&quot;,
  &quot;trigger&quot;: &quot;immediate&quot;,
  &quot;price&quot;: &quot;12.30000000&quot;,
  &quot;recordTimestamp&quot;: &quot;2023-02-10T12:03:27.197000Z&quot;
},
{
  &quot;fees&quot;: &quot;1.00&quot;,
  &quot;trigger&quot;: &quot;immediate&quot;,
  &quot;price&quot;: &quot;10.30000000&quot;,
  &quot;recordTimestamp&quot;: &quot;2023-03-10T12:03:27.197000Z&quot;
}

If I use mongoDB compass and do this filter

{recordTimestamp: {$gte: &#39;2023-01-24&#39;,$lte: &#39;2023-03-01&#39;}}

I get below expected result.

{
  &quot;fees&quot;: &quot;4.00&quot;,
  &quot;trigger&quot;: &quot;immediate&quot;,
  &quot;price&quot;: &quot;12.30000000&quot;,
  &quot;recordTimestamp&quot;: &quot;2023-02-10T12:03:27.197000Z&quot;
}

however when I use same filter in pymongo I get nothing.

stdateval = datetime(2023,1,24)
endateval = datetime(2023,3,1)
dtFilter = { &quot;recordTimestamp&quot; : {&quot;$gte&quot;: stdateval,&quot;$lt&quot;: 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[&#39;recordTimestamp&#39;], &quot;%Y-%m-%dT%H:%M:%S.%fZ&quot;)
	if stdateval &lt;= recordTimestamp &lt; 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 = &#39;2023-01-24&#39;
endateval = &#39;2023-03-01&#39;
dtFilter = { &quot;recordTimestamp&quot; : {&quot;$gte&quot;: stdateval,&quot;$lt&quot;: endateval}}

答案2

得分: 0

分享我找到的解决方案。

使用过滤器解决了。

{
   "$or" : [
       { 'recordTimestamp': {$gte: '2023-01-24'} },
       { 'recordTimestamp': {$lte: '2023-03-01'} }
   ]
}
英文:

Sharing solution I found.

resolved using filter

{
  &quot;$or&quot; : [
   { &#39;recordTimestamp&#39;: {$gte: &#39;2023-01-24&#39;} },
   { &#39;recordTimestamp&#39;: {$lte: &#39;2023-03-01&#39;} }
   ]  }

huangapple
  • 本文由 发表于 2023年6月2日 11:03:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定