【问题标题】:Reverse populate mongoose反向填充猫鼬
【发布时间】:2016-03-18 01:43:32
【问题描述】:

我试图弄清楚如何使用我的订单集合中的 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'
}
}); 

【问题讨论】:

    标签: node.js mongodb mongoose mean-stack mongoose-populate


    【解决方案1】:

    您可以使用Populate Virtuals 来实现反向填充。 (猫鼬版 > 4.5.0)

    案例请参考官方文档如上链接或https://stackoverflow.com/a/51664307/3210050

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 2015-07-13
      • 2016-10-10
      • 2019-09-16
      • 2015-07-13
      • 2020-01-17
      • 2021-05-03
      • 2021-01-09
      相关资源
      最近更新 更多