这是答案集。
var mongoose = require("mongoose");
var answerSchema = new mongoose.Schema ({
questionId:String,
username:String,
answered:Boolean
});
answerSchema.virtual('quest', {
ref: 'Question',
localField: 'questionId',
foreignField: '_id'
},{ toObject : {virtuals:true} }, { toJSON: { virtuals:true } });
及以下是我用来尝试填充"问题"的查询。基于id的模型。但是我只回复了"答案"收集,即使有基于foregin集合中的ID的数据。我究竟做错了什么 ?编辑:我使用的是Mongoose版本" ^ 4.6.7"
Answer.findOne({questionId:"58497379b0094008fe202b62"}).populate('quest').exec(function(error, vals) {
console.log(vals) }); // vals does not have the foregin collection related fields.