如何在同一条语句中使用填充和聚合? [英] how to use populate and aggregate in same statement?

查看:76
本文介绍了如何在同一条语句中使用填充和聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的约会收藏:

{ _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> = 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);

猫鼬API 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆