需求:在所有find查询的时候,默认添加查询参数 name:bennman

 

//创建一个query中间件 myMid.js
module.exports = function(schema){
  //这里可以是find findOne update等,详见:http://mongoosejs.com/docs/middleware.html
    schema.pre('findOne', function (next) {
     
     //给查询的query增加name:bennman
        this.where({name: 'bennman'})
        next()
    })
}

 

 

//使用中间件

var schema = new Schema({

})
schema.plugin(myMid)

module.exports = mongoose.model('xxxx', schema);

 

相关文章:

  • 2022-03-09
  • 2021-10-18
  • 2021-11-17
  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2021-08-22
  • 2022-02-07
  • 2022-12-23
  • 2022-02-28
相关资源
相似解决方案