【问题标题】:MongoDB last 30 days dataMongoDB 最近 30 天数据
【发布时间】:2017-07-18 01:41:25
【问题描述】:

我在 mongo 数据库中有这些数据,

{
    "_id": {
        "$oid": "5654a8f0d487dd1434571a6e"
    },
    "ValidationDate": {
        "$date": "2015-11-24T13:06:19.363Z"
    },
    "DataRaw": " WL 00100100012015-08-28 02:44:17+0000+ 16.81 8.879  1084.00",
    "ReadingsAreValid": true,
    "locationID": " WL 001",
    "Readings": {
        "pH": {
            "value": 8.879
        },
        "SensoreDate": {
            "value": {
                "$date": "2015-08-28T02:44:17.000Z"
            }
        },
        "temperature": {
            "value": 16.81
        },
        "Conductivity": {
            "value": 1084
        }
    },
    "HMAC": "ecb98d73fcb34ce2c5bbcc9c1265c8ca939f639d791a1de0f6275e2d0d71a801"
}

我的目标是计算过去 30 天内满足给定范围的温度、ph 值和电导率值,但我遇到了一个错误,我在网上搜索时无法解决。这是我的代码。

import datetime
from pymongo import MongoClient


def total_data_push():
    data = MongoClient().cloudtest.test_5_27

    now = datetime.datetime.utcnow()
    last_30d = now - datetime.timedelta(days=30)
    last_year = now.replace(year=now.year - 1)

    since_last_month = data.find({"ReadingsAreValid": False}, {"ValidationDate": {"$gte": last_30d}},
        {"Readings.temperature.value": {"$gt": 1.0}}).count()

    print since_last_month

def main():
    total_data_push()

if __name__ == "__main__":
    main()

当我在没有 ValidationDate 片段的情况下运行脚本时,它会返回正确的值,但添加此数据组件以获取过去 30 天的数据会返回以下错误

traceback (most recent call last):
  File "total_data_push.py", line 29, in <module>
    main()
  File "total_data_push.py", line 26, in main
    total_data_push()
  File "total_data_push.py", line 17, in total_data_push
    {"Readings.temperature.value": {"$gt": 1.0}}).count()
  File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 866, in find
    return Cursor(self, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 90, in __init__
    raise TypeError("skip must be an instance of int")
TypeError: skip must be an instance of int

我在这里真正想念什么?提前感谢您的帮助

【问题讨论】:

  • 我从来没有在 python 中使用过 Mongodb,所以我不确定客户端库是否更改了 find 方法的签名,但 mongo collection.find 只接受 2 个参数,过滤器和投影。您似乎定义了 3 个不同的过滤器对象,而不是一个组合 { { "ReadingsAreValid": False }, { "ValidationDate": { "$gte": last_30d } }, { "Readings.temperature.value": { "$gt ": 1.0 } } }

标签: python python-3.x mongodb pymongo pymongo-3.x


【解决方案1】:

这个过滤器应该是:

since_last_month = db.data.find({
"ReadingsAreValid": False,
"ValidationDate": {"$gte": last_30d},
"Readings.temperature.value": {"$gte": 1.0}
}).count()

【讨论】:

    【解决方案2】:

    正如@BenCr 所说,如果你看看find signature

    find(filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False,cursor_type=CursorType.NON_TAILABLE, sort=None,allow_partial_results=False,oplog_replay=False, 修饰符=无,操纵=真)

    filter 是第一个参数,如下所示:

    since_last_month = db.data.find({
        "ReadingsAreValid": False,
        "ValidationDate": {"$gte": last_30d},
        "Readings.temperature.value": {"$gte": 1.0}
        }).count()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2023-03-13
      相关资源
      最近更新 更多