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

Criteria#not mutates receiver

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.1.0.rc0, 7.0.6
    • Affects Version/s: 7.0.5
    • Component/s: Query
    • Labels:

      When I use Mongoid::Criteria.not future `where`s that are not chained to the `not` still use `not`

      require 'mongoid'
      
      Mongoid.connect_to 'query_not_causing_weird_stuff'
      
      class Player
        include Mongoid::Document
      
        field :name, type: String, default: ''
        field :position, type: String, default: ''
        field :team, type: String, default: ''
        field :number, type: Integer
      end
      
      Player.delete_all
      
      Player.create!(team: 'Falcons', position: 'QB', name: 'Vidur', number: 37)
      Player.create!(team: 'Falcons', position: 'WR', name: 'Jonathon', number: 47)
      Player.create!(team: 'Falcons', position: 'LB', name: 'David', number: 22)
      
      players_on_team = Player.where(team: 'Falcons')
      
      print players_on_team.where(position: 'WR').pluck(:name) # expect ['Jonathon']
      print players_on_team.not.where(position: 'WR').pluck(:name) # expect ['Vidur', 'David']
      print players_on_team.where(position: 'WR').pluck(:name) # expect ['Jonathon'] actual ['Vidur', 'David']
      

      Not sure if this has already been fixed but I couldn't find anything about it. I found this on mongoid 7.0.4 at it seems to still be a bug on 7.0.5

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

              Created:
              Updated:
              Resolved: