Mongo 中间件 pre find 修改query

需求:在所有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);

 

posted @ 2016-03-26 17:13  Unintended  阅读(758)  评论(0编辑  收藏  举报