【问题标题】:Is it possible to define a default_scope in Mongoose?是否可以在 Mongoose 中定义 default_scope?
【发布时间】:2013-05-11 04:06:19
【问题描述】:

在 Rails 中,default_scope (http://apidock.com/rails/ActiveRecord/Scoping/Default/ClassMethods/default_scope) 允许在从数据库中查询模型时指定应用的默认规则集。有没有办法在 Mongoose 中做同样的事情?

【问题讨论】:

    标签: node.js scope mongoose


    【解决方案1】:

    我认为不存在这样的东西,但您可以创建自己的版本,例如 find

    yourSchema.static('myFind', function() {
      var query = arguments[0];
      ...augment your query here...
      return this.find.apply(this, arguments);
    });
    

    你使用Model.myFind()而不是Model.find()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 2019-06-10
      • 2020-03-08
      • 1970-01-01
      相关资源
      最近更新 更多