
正文
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);








