Mongodb 无法在填充的数组中填充引用
我有以下模式:Mongodb 无法在填充的数组中填充引用,mongodb,mongoose,populate,Mongodb,Mongoose,Populate,我有以下模式: var SprintSchema = new Schema({ name: {type: String}, startDate: Date, endDate: Date, tasks: [{type: Schema.ObjectId, ref: 'Task'}] }); var TaskSchema = new Schema({ title: String, project: {type: Schema.ObjectId, ref:
var SprintSchema = new Schema({
name: {type: String},
startDate: Date,
endDate: Date,
tasks: [{type: Schema.ObjectId, ref: 'Task'}]
});
var TaskSchema = new Schema({
title: String,
project: {type: Schema.ObjectId, ref: 'Project'},
bp: Number,
status: {type: Number, default: 0},
description: String,
assignee: {type: Schema.ObjectId, ref: 'User'},
comments: [{
author: {type: Schema.ObjectId, ref: 'User'},
comment: String,
systemMessage: String,
time : { type : Date, default: Date.now}
}],
sDate: Date
});
var UserSchema = new Schema({
googleId: {type: String, default: ''},
firstName: {type: String, default: ''},
lastName: {type: String, default: ''},
email: {type: String, default: ''},
profilePicture: {type: String, default: '/images/user.png'},
accessToken: {type: String, default: ''},
role: {type: String, default: 'employee'}
});
现在,我想查询数据库中的特定sprint,所有任务都已完全填充。我的意思是sprint对象不仅应该包含已填充的任务,还应该填充每个任务的受让人(和项目)
我的代码如下:
Sprint.findById(req.params.id).populate('tasks').populate('tasks.assignee').exec(function(err, sprint) {
if(err) {
console.log('SprintView:: error while fetching the sprint: ' + err);
res.statusCode = 400;
res.send('NOT OK');
return;
}
for(var i = 0; i < sprint.tasks.length; i++) {
console.log(sprint.tasks[i]);
}
console.log(sprint);
});
Sprint.findById(req.params.id)。填充('tasks')。填充('tasks.assignee')。执行(函数(err,Sprint){
如果(错误){
log('SprintView::获取sprint时出错:'+err);
res.statusCode=400;
res.send(‘不正常’);
返回;
}
for(var i=0;i
但是受让人仍然没有被填充!我做错了什么?使用额外的填充命令修复了此问题:
Sprint.findById(req.params.id).populate('tasks').exec(function(err, sprint) {
User.populate(sprint, {path: 'tasks.assignee'}, function(err, sprint) {
Project.populate(sprint, {path: 'tasks.project'}, function(err, sprint) {
//Do something cool here
});
});
});