简体   繁体   中英

MongoDB last 30 days data

I have this data in a mongo database,

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

My goal is to calculate temperature, ph and conductivity values that satisfy a given range for the last 30 days but i am getting an error which i have not been able to resolve while searching online. Here is my code.

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()

When i run the script without the ValidationDate piece, it returns correct values but adding this data component to get that for last 30 days returns the following error

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

What am i really missing here? thanks for your help in advance

As said @BenCr, if you look at 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)

The filter is the first parameter as the following :

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

This filter should like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM