PyMon go aggregation Не работает с оператором $ max - PullRequest
1 голос
/ 29 мая 2020

Мой запрос отлично работает в оболочке mon go. Но при запуске через pymon go выдает ошибку. Может ли кто-нибудь помочь мне с этим.

db.collectioname.aggregate([
     {   "$match": { "$and": [ 
                        { "organization_id": int(organization_id) }, 
                        { "resulttime":{
                                "$gte":stdate,                                          
                                "$lte":enddate  
                            } 
                        }
                    ] 
            }
    },
    { "$skip" : int(offset) },
    { "$limit" : int(limit) }, 
    { "$group": { 
        "_id": "$userid",
        "max_temperature": { "$max": "$temperature" }, 
        "min_temperature": { "$min": "$temperature" } 
    }}
     ])

Однако я получаю сообщение об ошибке

pymongo.errors.OperationFailure: unknown operator: $max

1 Ответ

1 голос
/ 29 мая 2020

Пробовал; Он отлично работает для меня. Можете ли вы подтвердить, что этот образец кода работает? Если нет, напечатайте полную трассировку стека.

import pymongo
import datetime

offset = 0
limit = 1
organization_id = 1
stdate = datetime.datetime(2020, 1, 1, 0, 0)
enddate = datetime.datetime(2021, 1, 1, 0, 0)

db = pymongo.MongoClient()['mydatabase']
db.collectioname.insert_one({'organization_id': organization_id, 'resulttime': datetime.datetime.now(), 'temperature': 37.4})

records = db.collectioname.aggregate([
    {"$match": {"$and": [
        {"organization_id": int(organization_id)},
        {"resulttime": {
            "$gte": stdate,
            "$lte": enddate
        }}]}
    },
    {"$skip": int(offset)},
    {"$limit": int(limit)},
    {"$group": {
        "_id": "$userid",
        "max_temperature": {"$max": "$temperature"},
        "min_temperature": {"$min": "$temperature"}
    }}
])

print(list(records))
...