【发布时间】:2018-12-07 11:13:25
【问题描述】:
这是集合BlogPosts中的一个文档:
{
_id: ObjectId("..."),
post_title: "Hello World!",
post_body: "",
comments: [
{ user_id: ObjectId("123"), body: "nice post!" },
{ user_id: ObjectId("456"), body: "awesome!" },
]
}
我想显示带有用户名的 cmets,该名可在 Users 集合中的引用文档中找到:
{
_id: ObjectId("123"),
first_name: "Marion",
last_name: "Smith",
email_address: "marion@example.com",
password: "..."
}
有没有办法检索BlogPosts 文档,同时从该引用数据中包含first_name?
例如,我正在寻找这样的输出(每个评论都有一个名字):
{
_id: ObjectId("..."),
post_title: "Hello World!",
post_body: "",
comments: [
{ user_id: ObjectId("..."), first_name: "Marion", body: "nice post!" },
{ user_id: ObjectId("..."), first_name: "Margaret", body: "awesome!" },
]
}
我正在使用猫鼬。
【问题讨论】:
-
什么是mongodb版本?
-
@felipsmartins 我正在使用 MongoDB 4.0。
标签: mongodb mongodb-query aggregation-framework