猫鼬:使用方案属性填充投影

时间:2017-03-29 00:41:49

标签: node.js mongodb mongoose mongoose-schema mongoose-populate

我对以下人口/投影进行了分析,并且工作正常

  modelBaby.findOne({'userId': req.user.data._id, '_id': req.params.id})
  .populate('categories._categoryId', {
    levels:{
      $elemMatch:{
        name: 'PURPLE'
      }
    }
  })

但我需要将名称作为变量,例如:

modelBaby.findOne({'userId': req.user.data._id, '_id': req.params.id})
      .populate('categories._categoryId', {
        levels:{
          $elemMatch:{
            // over heree !!
            name: 'categories._levelName' or this._levelName
          }
        }
      })

方案参考

const babyCategory = new mongoose.Schema(
      {
        answer: {type: String},
        _categoryId: {type: Schema.Types.ObjectId, required: true, ref: 'category'},
        _categoryName: {type: String, required: true},
        _levelName: {type: String, required: true}
      },
      {versionKey: false, _id : false}
    );

0 个答案:

没有答案