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

Implement Arel-style "and", "or" and "not" combination of scopes

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

      Currently neither `and` nor `or` operations are correctly supported by Mongoid:

      irb(main):008:0> Band.where(x:2).and(Band.where(x:3))
      MONGODB | localhost:27741 | mongoid.find | STARTED | {"find"=>"bands", "filter"=>{"x"=>3}, "$clusterTime"=>{"clusterTime"=>#<BSON::Timestamp:0x0000563714d4f070 @seconds=1548439192, @increment=1>, "signature"=>{"hash"=><BSON::Binary:0x47397286344620 type=generic data=0x0000000000000000...>, "keyId"=>0}},...
      MONGODB | localhost:27741 | mongoid.find | SUCCEEDED | 0.001s
      => #<Mongoid::Criteria
        selector: {"x"=>2}
        options:  {}
        class:    Band
        embedded: false>
      
      irb(main):009:0> Band.where(x:2).or(Band.where(y:3))
      MONGODB | localhost:27741 | mongoid.find | STARTED | {"find"=>"bands", "filter"=>{"y"=>3}, "$clusterTime"=>{"clusterTime"=>#<BSON::Timestamp:0x0000563714d93630 @seconds=1548439212, @increment=1>, "signature"=>{"hash"=><BSON::Binary:0x47397286484620 type=generic data=0x0000000000000000...>, "keyId"=>0}},...
      MONGODB | localhost:27741 | mongoid.find | SUCCEEDED | 0.001s
      => #<Mongoid::Criteria
        selector: {"x"=>2}
        options:  {}
        class:    Band
        embedded: false>
      
      

      What should happen: https://guides.rubyonrails.org/active_record_querying.html#or-conditions

      Specifying the same field multiple times overwrites previous specifications:

      irb(main):010:0> Band.where(x:2).where(y:4)
      => #<Mongoid::Criteria
        selector: {"x"=>2, "y"=>4}
        options:  {}
        class:    Band
        embedded: false>
      
      irb(main):011:0> Band.where(x:2).where(x:4)
      => #<Mongoid::Criteria
        selector: {"x"=>4}
        options:  {}
        class:    Band
        embedded: false>
      

      Rails master has `and`, `or` and `not` implemented.

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

              Created:
              Updated:
              Resolved: