码迷,mamicode.com
首页 > 其他好文 > 详细

Mongo 中间件 pre find 修改query

时间:2016-03-26 18:55:55      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Mongo 中间件 pre find 修改query

标签:

原文地址:http://www.cnblogs.com/bennman/p/5323383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!