Mongodb 获取所有结果的整个子模式
Mongodb getting the entire sub-schema for all results
我有一个简单的架构如下,
Post {
createdAt: Date,
createdBy: ObjectID,
...
}
User {
_id: ObjectID,
name: String,
...
}
当用户创建这样的记录时,我将其与他的 ID 一起保存。
对于 Post 集合,我想创建一个查询并检索记录,并为所有结果发送整个用户模式。比如一个简单的结果:
[
{
createdAt: xxx,
createdBy: {
_id: xxx,
name: xxx
},
...
]
你可以用 population
来做到这一点,就像这样:
Posts.find({}).populate('createdBy');
我有一个简单的架构如下,
Post {
createdAt: Date,
createdBy: ObjectID,
...
}
User {
_id: ObjectID,
name: String,
...
}
当用户创建这样的记录时,我将其与他的 ID 一起保存。
对于 Post 集合,我想创建一个查询并检索记录,并为所有结果发送整个用户模式。比如一个简单的结果:
[
{
createdAt: xxx,
createdBy: {
_id: xxx,
name: xxx
},
...
]
你可以用 population
来做到这一点,就像这样:
Posts.find({}).populate('createdBy');