Javascript 多个群体 - mongoosejs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12821596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple populates - mongoosejs
提问by mrzmyr
Just a simple query, for example with a double ref in the model.
只是一个简单的查询,例如在模型中使用双参考。
Schema / Model
架构/模型
var OrderSchema = new Schema({
user: {
type : Schema.Types.ObjectId,
ref : 'User',
required: true
},
meal: {
type : Schema.Types.ObjectId,
ref : 'Meal',
required: true
},
});
var OrderModel = db.model('Order', OrderSchema);
Query
询问
OrderModel.find()
.populate('user') // works
.populate('meal') // dont works
.exec(function (err, results) {
// callback
});
I already tried something like
我已经尝试过类似的东西
.populate('user meal')
.populate(['user', 'meal'])
In fact only one of the populates works.
事实上,只有其中一个人有效。
So, how do is get two populates working ?
那么,如何让两个人口工作?
回答by JohnnyHK
You're already using the correct syntax of:
您已经在使用正确的语法:
OrderModel.find()
.populate('user')
.populate('meal')
.exec(function (err, results) {
// callback
});
Perhaps the meal
ObjectId from the order isn't in the Meals
collection?
也许meal
订单中的ObjectId 不在Meals
集合中?
回答by Michel Sahli
UPDATE:
This solution remains for the version 3.x of Mongoose
http://mongoosejs.com/docs/3.8.x/docs/populate.html
but is no longer documented for >= 4.x versions of Mongoose and so the answer from @JohnnyHK is the only valid one for now on.
更新:
此解决方案适用于 Mongoose
http://mongoosejs.com/docs/3.8.x/docs/populate.html 的3.x 版本,
但不再记录 >= 4.x 版本的 Mongoose 等答案来自@JohnnyHK 是目前唯一有效的。
ORIGINAL POST
If you're using Mongoose >= 3.6, you can pass a space delimited string of the path names to populate:
ORIGINAL POST
如果您使用的是 Mongoose >= 3.6,您可以传递一个以空格分隔的路径名字符串来填充:
OrderModel.find()
.populate('user meal')
.exec(function (err, results) {
// callback
});
回答by AlexanderDSmith
This has probably been resolved already, but this is my take on multiple & deep population in Mongodb > 3.6:
这可能已经解决了,但这是我对 Mongodb > 3.6 中多重和深度人口的看法:
OrderModel.find().populate([{
path: 'user',
model: 'User'
}, {
path: 'meal',
model: 'Meal'
}]).exec(function(err, order) {
if(err) throw err;
if(order) {
// execute on order
console.log(order.user.username); // prints user's username
console.log(order.meal.value); // you get the idea
}
});
There are probably other ways to do this, but this makes very readable code for beginners (like me)
可能还有其他方法可以做到这一点,但这为初学者(如我)提供了非常易读的代码
回答by Swapnil Soni
Latest mongoose v5.9.15
has ability to take array of populate fields
so you can do,
最新的猫鼬v5.9.15
能够获取填充字段的数组,因此您可以这样做,
.populate([ 'field1', 'field2' ])
回答by Franz Medrano
You can try:
你可以试试:
OrderModel.find()
.populate('user')
.populate('meal')
.exec(function (err, results) {
// callback
});
or with array options
或使用数组选项
OrderModel.find()
.populate([
{
path: "path1",
select: "field",
model: Model1
},
{
path: "path2",
select: "field2",
model: Model2
}
])
.exec(function (err, results) {
// callback
});
回答by Matthew Wolman
The best solution in my opinion is arrays when you are populating more than one foreign field on the same level. My code shows that I have multiple populates for different levels.
在我看来,当您在同一级别填充多个外部字段时,最好的解决方案是使用数组。我的代码显示我有不同级别的多个填充。
const patients = await Patient.find({})
.populate([{
path: 'files',
populate: {
path: 'authorizations',
model: 'Authorization'
},
populate: {
path: 'claims',
model: 'Claim',
options: {
sort: { startDate: 1 }
}
}
}, {
path: 'policies',
model: 'Policy',
populate: {
path: 'vobs',
populate: [{
path: 'benefits'
}, {
path: 'eligibility',
model: 'Eligibility'
}]
}
}]);
As you can see, wherever I needed more than one field of a document populated, I encased the populate key in an array and provided an array of objects, each object having a different path. Most robust and concise way to do it, in my opinion.
如您所见,在需要填充文档的多个字段的任何地方,我都会将 populate 键封装在一个数组中并提供一组对象,每个对象都有不同的路径。在我看来,最强大和简洁的方式来做到这一点。
回答by hamidreza nikoonia
i have same problem , but my mistake not in populate , i have an error in Model
我有同样的问题,但我的错误不是填充,我有模型错误
if you do this
如果你这样做
uncorrected
未修正
user: {
type: [Schema.Types.ObjectId],
ref: 'User'
}
correct
正确的
user: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
you must put array around of object like this
你必须像这样在对象周围放置数组