需求:在全部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);