猫鼬:在数组中填充对象的问题
Mongoose: issues populating an object in an array
我有以下三个模型:
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 对象 returns 填充的学生对象数组,但我只从每个学生对象中收到一个 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
我有以下三个模型:
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 对象 returns 填充的学生对象数组,但我只从每个学生对象中收到一个 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