如何从mongodb的模式数组中填充一个模式数组中的数据


const dummy1Schema = new mongoose.Schema({
testArr: [{
name: {
type: String
},
description: {
type: String
}
}]
})
const dummy2Schema = new mongoose.Schema({
testArr2: [{
name2: {
type: ObjectId,
ref: "dummy1Schema"
}
}]
})

我有两个类似的集合。现在,我想访问testArr2中的testArr数据。

我使用以下代码填充

dummy2Schema
.find()
.populate({
path: 'testArr2.name2',
model: 'dummy1Schema',
populate: {
path: 'testArr',
model: 'dummy1Schema'
}
})
.exec(function(err, data) {
if (err) throw err;
console.log(data);
});

name2给出空值

你可以像这样在你的控制器中填充它

Dummy2Schema.find ({}) .populate({路径:"testArr2.name2"})

最新更新