Mongodb聚合框架和Python

时间:2012-04-30 08:00:37

标签: python mongodb

我有下一个系列:

{"status": "new", "date": {"$date": 1334841845571}, "_id": {"$oid": "4f901223e4b0c2899fb22da0"}, "description": "description 1", "number": "01"}
{"status": "new", "date": {"$date": 1334841845571}, "_id": {"$oid": "4f90126fe4b0c2899fb22da1"}, "description": "description 1", "number": "02"}
{"status": "new", "date": {"$date": 1334841845571}, "_id": {"$oid": "4f901276e4b0c2899fb22da2"}, "description": "description 1", "number": "03"}
{"status": "blocked", "date": {"$date": 1332163445571}, "_id": {"$oid": "4f901286e4b0c2899fb22da3"}, "description": "description 1", "number": "04"}
{"status": "blocked", "date": {"$date": 1332163445571}, "_id": {"$oid": "4f90128ee4b0c2899fb22da4"}, "description": "description 1", "number": "05"}
{"status": "blocked", "date": {"$date": 1332163445571}, "_id": {"$oid": "4f901292e4b0c2899fb22da5"}, "description": "description 1", "number": "06"}

我正在尝试按“状态”列进行分组:

cursor = db.command('aggregate', table, pipeline=[
            {'$project': {u'date': 1, u'status': 1, u'number': 1, u'description': 1}}
            {'$group': {'_id': u'$status'}}])

但我明白了:

"ok"
"result"

为什么?

1 个答案:

答案 0 :(得分:1)

您需要将这些额外字段添加到管道中的“$ group”命令,但是您需要指定group命令应如何聚合它们,因为它不能将它们全部放在一个数组中。请提供您期望的输出结果。