我不知道 MongoEngine 是否支持聚合框架。
是否可以使用 MongoEngine 运行此查询?
db.collection.aggregate([
{ "$group": {
"_id": {
"year": { "$year": "$utc_timestamp" },
"month": { "$month": "$utc_timestamp" },
"day": { "$dayOfMonth": "$utc_timestamp" },
},
"defects": {
"$sum": { "$cond": [
{ "$eq": [ "$status", "defect" ] },
1,
0
]}
},
"totalCount": { "$sum": 1 }
}},
{ "$project": {
"defect_rate": {
"$cond": [
{ "$eq": [ "$defects", 0 ] },
0,
{ "$divide": [ "$defects", "$totalCount" ] }
]
}
}}
])
如果没有,我可以通过 MongoEngine 直接(原始)运行它吗?
最佳答案
您可以使用类上的 ._get_collection() 方法获取由 pymongo 驱动程序实现的原始“集合”对象。
Class._get_collection().aggregate([
{ '$group': {
'_id': {
'year': { '$year': '$utc_timestamp' },
'month': { '$month': '$utc_timestamp' },
'day': { '$dayOfMonth': '$utc_timestamp' },
},
'defects': {
'$sum': { '$cond': [
{ '$eq': [ '$status', 'defect' ] },
1,
0
]}
},
'totalCount': { '$sum': 1 }
}},
{ '$project': {
'defect_rate': {
'$cond': [
{ '$eq': [ '$defects', 0 ] },
0,
{ '$divide': [ '$defects', '$totalCount' ] }
]
}
}}
])
关于python - 如何使用 MongoEngine 运行这个 MongoDB 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24155429/