gpt4 book ai didi

python - Pymongo日期范围(相同日期不同时间)查询没有返回结果

转载 作者:太空宇宙 更新时间:2023-11-03 14:29:42 24 4
gpt4 key购买 nike

我是 pymongo 的新手,我想获取给定日期时间对象之间的数据。我给出了不同时间的相同日期意味着它不会返回结果,如果我给出了不同的日期意味着显示结果。我不知道为什么?所以,请任何人帮助我。我使用 MongoDB Compass,这里 ISODate 在 MongoDB Compass 中不可用,仅适用于 Date 对象,所以,我不需要 ISODate。任何人给我指导。

这是我的代码:

    start = datetime.datetime(2017,11,17,10,00,23)
end = datetime.datetime(2017,11,17,14,00,56)
data = db.datapoints.find({'res_date':{'$gte':start,'$lte':end }})

Mongo 文档如下:

{
_id:5a0c06aacff7580001e10fc9
res_value:"1"
line_code:"1"
res_date:2017-11-17 10:36:40.564
res_code:"1"
}
{
_id:5a0e6fcc409d8f00017f477c
res_value:"23"
line_code:"22"
res_date:2017-11-17 10:42:44.145
res_code:"22"
}
{
_id:5a0e6fcc409d8f00017f477c
res_value:"243"
line_code:"224"
res_date:2017-11-17 12:07:07.496
res_code:"223"
}
{
_id:5a0e6fcc409d8f00017f477c
res_value:"253"
line_code:"225"
res_date:2017-11-17 12:23:32.936
res_code:"522"
}

提前致谢!!

最佳答案

如果您没有添加特定的偏移量,mongodb 始终以 UTC 存储日期。

我尝试按照您提供的输入数据进行跟踪。这就是我添加的数据(与您的相同):

{ 
"_id" : ObjectId("5a0c06aacff7580001e10fc9"),
"res_value" : "1",
"line_code" : "1",
"res_date" : ISODate("2017-11-17T10:36:40.564+0000"),
"res_code" : "1"
}
{
"_id" : ObjectId("5a0e6fcc409d8f00017f477c"),
"res_value" : "23",
"line_code" : "22",
"res_date" : ISODate("2017-11-17T10:42:44.145+0000"),
"res_code" : "22"
}
{
"_id" : ObjectId("5a1427c36c9b762bd4961f30"),
"res_value" : "243",
"line_code" : "224",
"res_date" : ISODate("2017-11-17T12:07:07.496+0000"),
"res_code" : "223"
}
{
"_id" : ObjectId("5a1427d46c9b762bd4961f35"),
"res_value" : "253",
"line_code" : "225",
"res_date" : ISODate("2017-11-17T12:23:32.936+0000"),
"res_code" : "522"
}

有了这些数据和这个查询:

db.collectionName.find(
{
'res_date':{'$gte': ISODate("2017-11-17T10:00:23.564+0000"),
'$lte': ISODate("2017-11-17T14:00:56.564+0000") }
})

我得到了全部 4 个结果。

我认为问题不在于你的mongodb,而在于你的代码本身。查看此博客 https://julien.danjou.info/blog/2015/python-and-timezones

Always use aware datetime object, i.e. with timezone information. That makes sure you can compare them directly (aware and unaware datetime objects are not comparable) and will return them correctly to users. Leverage pytz to have timezone objects.

另请注意我添加的链接:

If you need to store those timestamps, the same rule should apply. If you rely on MongoDB, it assumes that all the timestamp are in UTC, so be careful when storing them – you will have to normalize the timestamp to UTC.

尝试使用这两个用例进行查询。这是一个例子

import datetime
import pytz

startUnaware= datetime.datetime(2017, 11, 17, 10, 00, 23)
endUnaware= datetime.datetime(2017, 11, 17, 14, 00, 56)
dataUnaware = db.datapoints.find({'res_date':{'$gte':startUnaware,'$lte':endUnaware }})

startAware = datetime.datetime(2017, 11, 17, 10, 00, 23, pytz.UTC)
endAware = datetime.datetime(2017, 11, 17, 14, 00, 56, pytz.UTC)
dataAware = db.datapoints.find({'res_date':{'$gte':startAware,'$lte':endAware }})

关于python - Pymongo日期范围(相同日期不同时间)查询没有返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47390554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com