需要帮助解决此问题,我正在尝试使用 populate 方法将 Id 替换为 reerenced 文档,但是当我输出返回的结果时,文档不会返回,而是 Id 值仍然存在。这是我要清除的架构和详细代码,谢谢
var CommentSchema=new mongoose.Schema({
comment:{type:String},
accountId:{type:mongoose.Schema.ObjectId, ref:'Account'}
});
var StatusSchema=new mongoose.Schema({
comments:[CommentSchema],
accountId:{type:mongoose.Schema.ObjectId, ref:'Account'},//should be replaced with docement
status:{type:String}
});
帐户架构
var AccountSchema=new mongoose.Schema({
email:{type:String,unique:true},
password:{type:String},
name:{
first:{type:String},
last:{type:String},
full:{type:String}
},
contacts:[Contact],
status:[Status]
});
var Account=mongoose.model('Account',AccountSchema);
var Comment=mongoose.model('Comment',CommentSchema);
var Status=mongoose.model('Status', StatusSchema);
//这就是我保存新评论的方式
app.post('/accounts/:id/status', function(req, res) {
var accountId = req.params.id;
models.Account.findById(accountId, function(account) {
account.save(function(err){
if (err) throw err;
var status=new models.Account.Status();
status.accountId=accountId;
status.status=req.param('status', '');
status.save(function(err){
account.status.push(status);
account.save(function(err){
if(err)console.log(err)
else
console.log('successful! status save...')
});
});
});
});
res.send(200);
});
//QUERY - 我希望这个查询将 accountId 替换为引用的文档,但是当查询运行时,accountId 仍然保存 id 而不是引用的文档
var getActivity= function(accountId,callback){
Account.findOne({_id:accountId}).populate('Status.accountId').exec(function(err, items) {
console.log('result is:'+items);
});
}