我在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
我在这里真正缺少什么?感谢您的帮助
答案 0 :(得分:1)
正如@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, modifiers = None,manipulate = True)
filter
是第一个参数,如下所示:
since_last_month = db.data.find({
"ReadingsAreValid": False,
"ValidationDate": {"$gte": last_30d},
"Readings.temperature.value": {"$gte": 1.0}
}).count()
答案 1 :(得分:0)
这个过滤器应该是:
since_last_month = db.data.find({
"ReadingsAreValid": False,
"ValidationDate": {"$gte": last_30d},
"Readings.temperature.value": {"$gte": 1.0}
}).count()