Lift single-branch $or conditions to top level

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Duplicate
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Query
    • None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      When $or produces a single branch, the query can be simplified to contain just the branch on the top level. For example:

      irb(main):008:0> Band.or(hello:1)
      => 
      #<Mongoid::Criteria
        selector: {"$or"=>[{"hello"=>1}]}
        options:  {}
        class:    Band
        embedded: false>
      
      # equivalent to
       => 
      #<Mongoid::Criteria
        selector: {"hello"=>1}
        options:  {}
        class:    Band
        embedded: false>
      

      Note that, per https://www.mongodb.com/docs/mongoid/master/reference/queries/#logical-operations, this simplification must only be performed when the left side of .or doesn't have any conditions and right side has only one condition, i.e. it is insufficient to just check the arguments to .or.

              Assignee:
              Unassigned
              Reporter:
              Oleg Pudeyev (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: