Finding mongoDB records in batches (using mongoid ruby adapter)

后端 未结 6 1957
悲哀的现实
悲哀的现实 2020-12-14 00:32

Using rails 3 and mongoDB with the mongoid adapter, how can I batch finds to the mongo DB? I need to grab all the records in a particular mongo DB collection and index them

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 01:02

    If you are iterating over a collection where each record requires a lot of processing (i.e querying an external API for each item) it is possible for the cursor to timeout. In this case you need to perform multiple queries in order to not leave the cursor open.

    require 'mongoid'
    
    module Mongoid
      class Criteria
        def in_batches_of(count = 100)
          Enumerator.new do |y|
            total = 0
    
            loop do
              batch = 0
    
              self.limit(count).skip(total).each do |item|
                total += 1
                batch += 1
                y << item
              end
    
              break if batch == 0
            end
          end
        end
      end
    end
    

    Here is a helper method you can use to add the batching functionality. It can be used like so:

    Post.all.order_by(:id => 1).in_batches_of(7).each_with_index do |post, index|
      # call external slow API
    end
    

    Just make sure you ALWAYS have an order_by on your query. Otherwise the paging might not do what you want it to. Also I would stick with batches of 100 or less. As said in the accepted answer Mongoid queries in batches of 100 so you never want to leave the cursor open while doing the processing.

提交回复
热议问题