根据 mongoDb 的小时数提取数据

Extract data based on hours from mongoDb

我正在尝试从 MongoDB 中提取数据。

我的原始 Db 文件有一个包含日期和时间的查询(查询名称 'updateTime')。

数据每 1 分钟更新一次。

为了将此数据库数据与其他数据匹配,我只需要从每小时更新中提取数据。

例如,我只需要来自以下更新查询的数据:

"updateTime" : "2021-11-30 20:00:00", 

"updateTime" : "2021-11-30 21:00:00", 

"updateTime" : "2021-11-30 22:00:00", 
.............

我不需要每分钟的数据,现在我的 python 代码从每分钟提取数据:

I have code here --- login to DD and loop to sub DB

After the loop, I used the following code to extract data based on query cno.

query = {
    'cno': 10,
 
}
projection = {  '_id':False,
              'updateTime': True,
                'cno': True,
                'pressure':True,
                'radius':True,
                'items.typeA':True}

我在 MongoDB 主页中搜索了有关提取数据的内容,并找到了 'aggregation'(https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromString/),但没有任何想法可以应用到我的 'projection' 部分。

我怎样才能提取每小时更新一次的数据?

有什么帮助或建议吗?

谢谢。

您可以像这样在查询中添加一行:

query = {
    'cno': 10,
    'updatetime': {'$regex':'000$'} #if time is ended with 000.
 
}

关于 $regex : https://docs.mongodb.com/manual/reference/operator/query/regex/

它为查询中的模式匹配字符串提供正则表达式功能