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

Cannot update more than 101 items in a Criteria#each loop

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.1
    • Affects Version/s: None
    • Component/s: None
    • None

      This bug popped up after upgrading to v4.0.1 and it's present in v4.0.2. Most likely it's related to MONGOID-3847

      Sample code (mongoid_issue.rb):

      gem 'mongoid', '=4.0.2' # same for 4.0.1
      require 'mongoid'
      
      Mongoid.configure do |c|
        c.connect_to "test"
      end
      Mongoid::QueryCache.enabled = true
      
      p "Using Mongoid #{Mongoid::VERSION}"
      
      class MyDocument
        include Mongoid::Document
      
        field :parent_id, type: String
        field :prop, type: Integer
      end
      
      def create_docs
        n = ENV['N'].to_i
        n.times do |i|
          MyDocument.create(parent_id: i % 10)
        end
      end
      
      def clean_up
        MyDocument.delete_all
      end
      
      def execute
        clean_up
        create_docs
      
        Mongoid.logger = Logger.new($stdout)
        Mongoid.logger.level = Logger::DEBUG
        Moped.logger = Mongoid.logger
      
        yield
      
        clean_up
      end
      
      def main
        execute do
          MyDocument.where(:parent_id.gte => 0).each do |doc|
            doc.set(prop: Random.rand(100))
          end
        end
      rescue => ex
        p ex.message
        puts ex.backtrace.join("\n")
      end
      
      main
      

      for {{ N=101 ruby mongoid_issue.rb }} it succeeds

      for {{ N=102 ruby mongoid_issue.rb }} i get the following:

      ~/lab/mongoid-issue$ N=102 ruby mongoid_issue.rb 
      "Using Mongoid 4.0.2"
      ...
      # [ lots of MOPED UPDATE commands ]
      ...
      D, [2015-03-03T16:37:32.024320 #17685] DEBUG -- :   MOPED: 127.0.0.1:27017 GET_MORE     database=test collection=my_documents limit=0 cursor_id=856692613554 runtime: 0.8233ms
      "undefined method `push' for nil:NilClass"
      /home/elod/.rvm/gems/ruby-2.2.0/gems/mongoid-4.0.2/lib/mongoid/query_cache.rb:141:in `with_cache'
      /home/elod/.rvm/gems/ruby-2.2.0/gems/mongoid-4.0.2/lib/mongoid/query_cache.rb:238:in `get_more'
      /home/elod/.rvm/gems/ruby-2.2.0/gems/moped-2.0.4/lib/moped/cursor.rb:31:in `each'
      /home/elod/.rvm/gems/ruby-2.2.0/gems/moped-2.0.4/lib/moped/query.rb:77:in `each'
      /home/elod/.rvm/gems/ruby-2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:122:in `each'
      /home/elod/.rvm/gems/ruby-2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual.rb:20:in `each'
      mongoid_issue.rb:44:in `block in main'
      mongoid_issue.rb:37:in `execute'
      mongoid_issue.rb:43:in `main'
      mongoid_issue.rb:53:in `<main>'
      

      Seems like it fails when tries to get the second batch of items. This happens only if you try modify the document.

      The issue is not happening if Mongoid::QueryCache.enabled is set to false

            Assignee:
            dianna.hohensee@mongodb.com Dianna Hohensee (Inactive)
            Reporter:
            bejmuller bejmuller
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: