0
我了深刻的填充面臨的問題,蒙戈,Node.js的深填充
var commentSchema = mongoose.Schema({
name: String });
var userSchema = mongoose.Schema({
userId: { type: String, default: '' },
comments: [subSchema]
});
var socialSchema = mongoose.Schema({
userId: {type: mongoose.Schema.Types.ObjectId, ref: "user"},
countryCode: { type: String, default: '' },
socialId: {type: mongoose.Schema.Types.ObjectId, ref: "user"},
dateAdded: { type: Date, default: Date.now }
});
目的是填充「社會」的文件,並獲得具有匹配id的評論。解決方案是將socialId的「ref」設置爲「user.comments」,而不僅僅是用戶。這樣它總是返回null。
Social.find().populate('socialId').exec(function(err, feed) {
if (err) {
console.log(err);
throw err;
}else{
res.json({data: feed, message:"Getting feed"});
}
})