Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4808

Require that Criteria strategies are explicitly requested, use $and behavior by default for all/in/nin

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.1.0.rc0
    • Affects Version/s: None
    • Component/s: Query
    • None

      Mongoid implements strategies called override, union and intersect. These are available as methods on Criteria. These are presently undocumented (union is given once in an example in the tutorial with no explanation how it works, intersect is not mentioned at all) despite being applied by default (union to #all, intersect to #in and #nin). These strategies alter the way the next condition is added to the criteria, similarly to one of the forms of the #not method. These methods only handle specific forms of adding operators to queries, for example they handle the #in method but not the $in operator:

      irb(main):024:0> Band.where(foo:{'$in'=>[3]}).union.where(foo:{'$in'=>[4]})
      => #<Mongoid::Criteria
        selector: {"foo"=>{"$in"=>[]}}
        options:  {}
        class:    Band
        embedded: false>
      
      irb(main):025:0> Band.where(foo:{'$in'=>[3]}).union.in(foo:[4])
      => #<Mongoid::Criteria
        selector: {"foo"=>{"$in"=>[3, 4]}}
        options:  {}
        class:    Band
        embedded: false>
      
      

      union on criteria overall does not do anything (or behaves incorrectly depending on point of view):

      irb(main):028:0> Band.where(foo:3).union.where(foo:4)
      => #<Mongoid::Criteria
        selector: {"foo"=>4}
        options:  {}
        class:    Band
        embedded: false>
      

      This ticket is to:

      Document strategies overall, pointing out that they are only respected by a handful of operators (all/in/nin) and only in some of the ways of using these operators (via query methods)
      Change default behavior of operators supporting strategies to use the simple addition, i.e. join with $and to existing conditions
      Provide examples in the tutorial of specifying all of the available strategies and illustrating how the strategy changes the generated query
      Point out that strategies expand Range on client side

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: