1

我的查询在 mongo shell 上运行良好。但是当通过 pymongo 运行时会出错。有人可以帮我解决这个问题。

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
4

1 回答 1

1

我尝试过这个; 这对我来说可以。您能否确认此示例代码有效。如果不打印完整的堆栈跟踪。

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))
于 2020-05-29T14:36:37.483 回答