gpt4 book ai didi

node.js - 反向填充 Mongoose

转载 作者:可可西里 更新时间:2023-11-01 09:55:12 25 4
gpt4 key购买 nike

我正在尝试了解如何使用我的订单集合中的 businessId 属性来填充我的业务订单。

我试过了,但无法正常工作。 https://www.npmjs.com/package/mongoose-reverse-populate

知道我做错了什么或关于如何完成我需要做的事情的其他建议吗?

Business.findById(id).exec(function(err, business) {

var opts = {
modelArray: business,
storeWhere: "orders",
arrayPop: true,
mongooseModel: Order,
idField: "businessId"
};

reversePopulate(opts, function(err, businessAndOrders) {
req.business = businessAndOrders;
next();
});
});

--

var BusinessSchema = new Schema({
businessName:{
type:String
}
/*.......*/

});

var OrderSchema = new Schema({
data: {
type: String,

}
created: {
type: Date,
default: Date.now
},
businessId: {
type: Schema.ObjectId,
ref: 'Business'
}
});

最佳答案

您可以使用 Populate Virtuals实现反向填充。 ( Mongoose 版本 > 4.5.0)

案例请引用上面链接的官方文档或https://stackoverflow.com/a/51664307/3210050

关于node.js - 反向填充 Mongoose ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34235491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com