【发布时间】:2021-09-17 01:37:17
【问题描述】:
我有以下架构,其中项目类型可能会有所不同,并在 connections.kind 中提到。
var userSchema = new Schema({
name: String,
connections: [{
kind: String,
item: { type: ObjectId, refPath: 'connections.kind' }
}]
});
var organizationSchema = new Schema({ name: String });
我正在尝试进行动态查找,以便填充项目对象。但这似乎不起作用。
db.users.aggregate([
{
$lookup:{
from: '$connections.kind',
localField: 'connections.item',
foreignField: '_id',
as: 'items'
}
}
])
我知道我可以用 mongoose.populate 做到这一点,但想知道 $lookup 是否有可能
【问题讨论】:
-
您可以使用填充 db.user.find({}).populate({path:"connections.item"})
-
from将具有集合的名称。