- 
    Type:
Bug
 - 
    Resolution: Fixed
 - 
    Priority:
Major - P3
 - 
    Affects Version/s: 7.2.5, 7.1.11, 7.3.3
 - 
    Component/s: Query
 
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}]}
- clones
 - 
                    
MONGOID-5183 Existing `$and` clause in query is overwritten by `where` condition
-         
 - Closed
 
 -         
 
- is duplicated by
 - 
                    
RUBY-2788 Overrides precedent other '$and' expression
-         
 - Closed
 
 -