mongodb aggregation在mongoengine的實現

管道定義類似,就是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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章