3

これは私の予定のコレクションです

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

どうすればこのような結果を得ることができますか

{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));
});

助けてください

4

1 に答える 1

9

$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}}
], ...)
于 2013-05-19T03:39:36.760 に答える