【问题标题】:Mongoose | Keep the same Population to all functions猫鼬 |对所有功能保持相同的人口
【发布时间】:2019-10-28 02:40:20
【问题描述】:

我的作物模型有这个架构

var CropSchema = new mongoose.Schema({
    name: String,
    zones: [{
        type: Schema.Types.ObjectId,
        ref: 'zone'
    }],
    ...
});

我的区域模型的这个架构

var ZoneSchema = new mongoose.Schema({
    name: String,
    poor: [{
        type: Schema.Types.ObjectId,
        ref: 'condition'
    }],
    ...
});

我的条件模型的这个架构

var ConditionSchema = new mongoose.Schema({
    name: String,
    action_on_controls: [{
        type: Schema.Types.ObjectId,
        ref: 'control'
    }],
    ...
});

我的控制模型的这个架构

var ControlSchema = new mongoose.Schema({
    name: String,
    ...
});

我在 Node 中获取所有作物的方式是这样的:

  public index(req: Request, res: Response) {
    return Crop.find().populate('zones').populate({
      path: 'zones',
      populate: [
        {
          path: 'poor', populate: [
            { path: 'action_on_controls' }]
        }
      ]
    }).exec()
      .then(respondWithResult(res, 200))
      .catch(handleError(res, 500));
  }

我获得单个作物的方式是这样的:

  public show(req: Request, res: Response) {
    return Crop.findById(req.params.id).populate({
      path: 'zones',
      populate: [
        {
          path: 'poor', populate: [
            { path: 'action_on_controls' }]
        }
      ]
    }).exec()
      .then(handleEntityNotFound(res))
      .then(respondWithResult(res, 200))
      .catch(handleError(res, 500));
  }

如您所见,部分:

.populate({..})

重复两次。

如何保持相同的填充配置,这样我就不必一直编写/更新相同的东西?

【问题讨论】:

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


    【解决方案1】:

    您可以将填充对象保存为变量并共享它:

    const zonePopulateObj = {
      path: 'zones',
      populate: [
        {
          path: 'poor', populate: [
            { path: 'action_on_controls' }]
        }
      ]
    };
    

    然后在你的查询中

    return Crop.find().populate(zonePopulateObj).exec();
    
    return Crop.findById(req.params.id).populate(zonePopulateObj).exec();
    

    或者您可以将查询逻辑拉入一个新函数并共享它

    public index(req: Request, res: Response) {
        return findCrop()
          .then(respondWithResult(res, 200))
          .catch(handleError(res, 500));
      }
    
    
    public show(req: Request, res: Response) {
        return findCrop(req.params.id)
          .then((array)=>array.length ? array[0] : {})
          .then(handleEntityNotFound(res)) // may need to update this function not sure how it checks for not found.
          .then(respondWithResult(res, 200))
          .catch(handleError(res, 500));
      }
    
    
    
      const findCrop = (id)=>{
          let queryObj = {};
          if(id){
              queryObj._id=id
          }
          return Crop.find(queryObj).populate({
            path: 'zones',
            populate: [
              {
                path: 'poor', populate: [
                  { path: 'action_on_controls' }]
              }
            ]
          }).exec()
      }
    

    我个人更喜欢第一种方法。

    【讨论】:

      猜你喜欢
      • 2015-12-11
      • 2019-10-27
      • 2017-11-19
      • 2015-05-14
      • 2021-03-10
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 2021-04-12
      相关资源
      最近更新 更多