我面临着一个人口密集的问题,
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的注释。解决方案是设置" ref" of socialId to" 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"});
}
})
答案 0 :(得分:0)
据我所知,您可能需要再次填充它们我不认为只有一个查询可以实现深度人口。
Social.find().populate('socialId').exec(function(err, feed) {
if (err) {
console.log(err);
throw err;
}else {
Comments.populate(feed, {path: 'socialId.user.comments'}, function (err, populatedFeeds) {
if (err) {
console.log(err);
throw err;
} else {
res.json({data: feed, message: "Getting feed"});
}
})
}
})
答案 1 :(得分:0)
这个mongoose-deep-populate
var deepPopulate = require('mongoose-deep-populate')(mongoose);
var Schema = new mongoose.Schema({
...
});
Schema.plugin(deepPopulate);
// while populating:
Schema.find(...).deepPopulate({
paths: [], // list of paths goes here
{
populate: {
'first.path': 'things to select' // space seperated list of fields to select
}
}
});
您可以阅读有关此here
的更多信息