【发布时间】:2013-05-11 04:06:19
【问题描述】:
在 Rails 中,default_scope (http://apidock.com/rails/ActiveRecord/Scoping/Default/ClassMethods/default_scope) 允许在从数据库中查询模型时指定应用的默认规则集。有没有办法在 Mongoose 中做同样的事情?
【问题讨论】:
在 Rails 中,default_scope (http://apidock.com/rails/ActiveRecord/Scoping/Default/ClassMethods/default_scope) 允许在从数据库中查询模型时指定应用的默认规则集。有没有办法在 Mongoose 中做同样的事情?
【问题讨论】:
我认为不存在这样的东西,但您可以创建自己的版本,例如 find:
yourSchema.static('myFind', function() {
var query = arguments[0];
...augment your query here...
return this.find.apply(this, arguments);
});
你使用Model.myFind()而不是Model.find()。
【讨论】: