我有以下三个模型:
var User = {
first_name: String,
last_name: String,
}
var Student = {
role = String,
user = {type: mongoose.Schema.Types.ObjectId, ref: 'User'}
groups = [{type: mongoose.Schema.Types.ObjectId, ref: 'Group'}],
}
var Group = {
name = String,
students = [{type: mongoose.Schema.Types.ObjectId, ref: 'Student'}],
}
我的快速获取方法如下所示:
router.route('/')
.get(function(req, res){
Group.find().populate('students').exec(function(err, groups){
res.json(groups);
});
我的 json 对象返回填充的学生对象 a 的数组,但我只从每个学生对象中接收 user._id 。我怎样才能填充用户对象?任何信息都会很棒!谢谢
最佳答案
您可以跨多个级别进行填充:
router.route('/')
.get(function(req, res){
Group
.find()
.populate({
path: 'students',
// Get the student's user ids
populate: { path: 'user' }
})
.exec(function(err, groups){
res.json(groups);
});
您可以阅读更多相关信息here
关于javascript - Mongoose:在数组中填充对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847988/