如何在同一个语句中使用填充和聚合?
问题描述:
这是我的约会集合:
{ _id: ObjectId("518ee0bc9be1909012000002"), date: ISODate("2013-05-13T22:00:00Z"), patient:ObjectId("518ee0bc9be1909012000002") }
{ _id: ObjectId("518ee0bc9be1909012000002"), date: ISODate("2013-05-13T22:00:00Z"), patient:ObjectId("518ee0bc9be1909012000002") }
{ _id: ObjectId("518ee0bc9be1909012000002"), date: ISODate("2013-05-13T22:00:00Z"), patient:ObjectId("518ee0bc9be1909012000002") }
我使用聚合得到以下结果
I used aggregate to get the following result
{date: ISODate("2013-05-13T22:00:00Z"),
patients:[ObjectId("518ee0bc9be1909012000002"),ObjectId("518ee0bc9be1909012000002"),ObjectId("518ee0bc9be1909012000002")] }
像这样:
Appointments.aggregate([
{$group: {_id: '$date', patients: {$push: '$patient'}}},
{$project: {date: '$_id', patients: 1, _id: 0}}
], ...)
如何填写患者文档我证实了这一点,但它不起作用... Appointments.find({}).populate("patient").aggregate
....
How can I populate the patient document
I trued this but it doesn't work ... Appointments.find({}).populate("patient").aggregate
....
换句话说,我可以在同一个语句中使用填充和聚合
In other words, can i use populate and aggregate at the same statement
请帮忙
答
使用最新版本的 mongoose (mongoose >= 3.6),您可以但它需要第二个查询,并且使用不同的填充.聚合后,执行以下操作:
With the latest version of mongoose (mongoose >= 3.6), you can but it requires a second query, and using populate differently. After your aggregation, do this:
Patients.populate(result, {path: "patient"}, callback);
在 Mongoose API 和 猫鼬文档.