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

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

问题描述

这是我的约会集合:

{ _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猫鼬文档.

这篇关于如何在同一个语句中使用填充和聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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