需要幫助解決這個問題,我想用填充方法來取代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結果 –
問題仍然存在 –