Mongo 中间件 pre find 修改query

需求:在全部find查询的时候,默认添加查询参数 name:bennmanhtml

 

//建立一个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);
相关文章
相关标签/搜索