管道定義類似,就是key都要加上引號
pipeline = [
{
'$match': {'InnerMark': 'No'}
},
{
'$project': {
"D": "$DiskSpace",
"M": "$MemoryLimit",
'cmp': {
'$and': [
{
'$lte': ["$BusinessCreateMonth", month]
},
{
'$or': [
{'$gt': ["$BusinessDeleteMonth", month]},
{'$eq': ['$DeleteTime', None]}
]
}
]
}
}
},
{
'$match': {'cmp': True}
},
{
'$group': {
'_id': "$cmp",
'count': {'$sum': 1},
'disk_count': {'$sum': '$D'},
'memory_count': {'$sum': '$M'}
}
}
]
# 創建遊標,這裏的_get_collection是受保護對象(上下是一樣的的,注意那個星號)
cur = Db._get_collection().aggregate(pipeline)
cur = Db.objects().aggregate(*pipeline)
# 聚合結果存下來
result = cur.next()
# 關閉遊標
cur.close()
# 返回結果
return result