2012-04-30 142 views
0

我的下一個集合:MongoDB的聚合框架和Python

{"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" 

爲什麼?

+0

好的我發現結果是光標['結果']但我沒有我在$ project中指出的列 {u'ok':1.0,u'result':[{ u'_id ':u'new'},{u'_id ':u'blocked'}]} - 它只顯示_id列,但我需要的日期,狀態,數量,說明以及 – KennyPowers

回答

1

您將需要這些額外的字段在您的管道添加到「$組」命令,但你需要指定group命令應該如何對其進行聚合,因爲它不能只是把它們都在同一個陣列。請提供您期望的輸出。

+0

我發現$第一和$最後的運營商,所以如果我的結果,然後由cloumn組排序他們,我可以使用$第一/ $最後的運營商來定義我想有結果的列。所以它解決了,無論如何感謝幫助。 – KennyPowers