[英]Mongo, Node.js deep populate
I am facing with a deep populating problem, 我面临着一个人口稠密的问题,
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 }
});
The goal is to populate "social" document and get the comments with the matching id. 目标是填充“社交”文档并获取具有匹配ID的注释。 The solution would be to set the "ref" of the socialId to "user.comments" and not just user.
解决方案是将socialId的“ ref”设置为“ user.comments”,而不仅仅是user。 This way it always return null.
这样,它总是返回null。
Social.find().populate('socialId').exec(function(err, feed) {
if (err) {
console.log(err);
throw err;
}else{
res.json({data: feed, message:"Getting feed"});
}
})
To my knowledge you may need to populate them again I don't think deep population is possible in just one query. 据我所知,您可能需要再次填充它们,我认为仅凭一个查询就不可能进行大量填充。
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"});
}
})
}
})
There is a mongoose plugin for this mongoose-deep-populate
这个
mongoose-deep-populate
有一个mongoose插件
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
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.