When multiple conditions on the same field are given in separate method invocations, Mongoid uses `$and` to store the newer conditions, e.g.:
irb(main):012:0> Band.where(id: 1).and({year: {'$in' => [2020]}}, {year: {'$in' => [2021]}}) => #<Mongoid::Criteria selector: {"_id"=>1, "year"=>{"$in"=>[2020]}, "$and"=>[{"year"=>{"$in"=>[2021]}}]} options: {} class: Band embedded: false>
Here, there are two conditions on `year` and they are combined with `$and`.
When `where` is used and is given a field which already exists in the criteria with the same operator, and the criteria also already contains an `$and` clause, Mongoid uses `$and` to add the new condition from `where` but in doing so apparently clobbers the existing `$and` clause, as follows:
irb(main):014:0> Band.where(id: 1).and({year: {'$in' => [2020]}}, {year: {'$in' => [2021]}}).where(id: 2 ) => #<Mongoid::Criteria selector: {"_id"=>1, "year"=>{"$in"=>[2020]}, "$and"=>[{"_id"=>2}]} options: {} class: Band embedded: false>
--------------------------------
Original report:
I've met the problem that the later expression is overriding precedent other '$and' expressions if expressions are appeared in the specific oder like this:
# this is OK Band.where(id: 1).where(id: 2).selector => {"_id"=>1, "$and"=>[{"_id"=>2}]} # this is also OK Band.and({year: {'$in' => [2020]}}, {year: {'$in' => [2020]}}).selector => {"year"=>{"$in"=>[2020]}, "$and"=>[{"year"=>{"$in"=>[2020]}}]} # But this is bad, I think Band.where(id: 1).and({year: {'$in' => [2020]}}, {year: {'$in' => [2020]}}).where(id: 2).selector => {"_id"=>1, "year"=>{"$in"=>[2020]}, "$and"=>[{"_id"=>2}]}
- is cloned by
-
MONGOID-5236 Add Feature Flag: Existing `$and` clause in query is overwritten by `where` condition
- Closed
- is duplicated by
-
RUBY-2788 Overrides precedent other '$and' expression
- Closed