需要帮助解决这个问题,我想用填充方法来取代Id的一个reerenced文档,但是当我输出返回的结果时,文档不会返回,而是Id值仍然保持。这里是我的架构和细节代码clearify,感谢填充方法不返回文档 - Mongoose
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}
});
账户SCHEMA
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 -I想到这查询以用所引用的文档替换accountId,但是当查询运行时,accountId仍然保存id而不是引用的文档
var getActivity= function(accountId,callback){
Account.findOne({_id:accountId}).populate('Status.accountId').exec(function(err, items) {
console.log('result is:'+items);
});
}
我认为这应该只是'填入(“状态”)' – Benoir
感谢的是,我仍然有thesame结果 –
问题仍然存在 –