PyMongo 聚合不适用于 $max 运算符
PyMongo aggregation Not working with $max operator
我的查询在 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
我试过了;这对我来说可以。你能确认这个示例代码有效吗?如果不打印完整的堆栈跟踪。
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))
我的查询在 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
我试过了;这对我来说可以。你能确认这个示例代码有效吗?如果不打印完整的堆栈跟踪。
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))