2013-05-19 148 views
2

這是我的任命收集集團按日期貓鼬

{ _id: ObjectId("518ee0bc9be1909012000002"), date: ISODate("2013-05-13T22:00:00Z"), patient:"John" } 

{ _id: ObjectId("518ee0bc9be1909012000002"), date: ISODate("2013-05-13T22:00:00Z"), patient:"alex" } 

{ _id: ObjectId("518ee0bc9be1909012000002"), date: ISODate("2013-05-13T22:00:00Z"), patient:"sara" } 

我怎樣才能讓我的resualt這樣

{date: ISODate("2013-05-13T22:00:00Z"), 
patients:["John","alex","sara"] } 

我試試這個

Appointments.aggregate([ 
{ 
$group: { 
_id: '$date' 
} 
} 
], function(err, doc) { 
return console.log(JSON.stringify(doc)); 
}); 

任何幫助,請

回答

5

使用$push集成算組裝patients數組中的$group

Appointments.aggregate([ 
    {$group: {_id: '$date', patients: {$push: '$patient'}}}, 
    {$project: {date: '$_id', patients: 1, _id: 0}} 
], ...) 

要包括患者ID在你的後續問題:

Appointments.aggregate([ 
    {$group: {_id: '$date', patients: {$push: { 
     patient: '$patient' 
     _id: '$_id' 
    }}}}, 
    {$project: {date: '$_id', patients: 1, _id: 0}} 
], ...) 
+0

感謝.... 如何推動_id與病人? ?? {日期:ISODate( 「2013-05-13T22:00:00Z」), 患者: [{_id:518ee0bc9be1909012000002,患者: 「約翰」},{ _id:518ee0bc9be1909012000002,患者: 「亞歷克斯」}, {_id:518ee0bc9be1909012000002,患者:「薩拉」}] } –

+0

@MnSa查看更新的答案。 – JohnnyHK

+0

非常感謝你... –