2017-02-27 79 views
1

我有一个蒙戈数据库这个数据,MongoDB的最近30天的数据

{ 
    "_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 

什么我真的很想在这里?感谢提前

+1

我从来没有从python使用过Mongodb,所以我不确定客户端库是否更改find​​方法的签名,但是mongo collection.find只接受2个参数,即过滤器和投影。你似乎有定义的,而不是一个组合 {{ \t \t “ReadingsAreValid” 3种不同的滤镜对象: “$ GTE”{ \t \t \t:假 \t}, \t { \t \t “ValidationDate” last_30d \t \t} \t}, \t { \t \t “Readings.temperature.value”:{ \t \t \t“$ gt”:1.0 \t \t} \t} } – BenCr

回答

1

由于你的帮助表示@BenCr,如果你看一下find signature

发现(过滤器=无,投影=无,跳过= 0,上限= 0, no_cursor_timeout =假cursor_type = CursorType.NON_TAILABLE, 排序=无,allow_partial_results =假,oplog_replay =假, 改性剂=无,操纵=真)

filter是如下面的第一个参数:

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