Platinum Partner
ruby,architects,nosql,tutorial,mongo

A Tutorial on Paging and Iterating Over Large MongoDB Collection

Sometimes you need to iterate over a large MongoDB collection. The biggest issue is that, by default, cursors timeout after 10 minutes of inactivity. For very large collections it’s not uncommon to take longer than that to process results and you get an exception half way through the iteration. A cursor is a server-side construct, how about a client-side cursor?

Here’s a Mongo Ruby iterator that will call Mongo::Collection.find in increments.

    module Mongo
      class Collection
        def find_all(query = {}, by = 1000, &block)
          idx = 0
          while ((results = find(query, { :limit => by, :skip => idx })) && results.count(true) > 0)
            results.each do |result|
              yield result
              idx += 1
            end
          end
          self
        end
      end
    end

And a Mongoid iterator built into Mongoid::Criteria.

 

    module Mongoid
      class Criteria
        def each_by(by = 1000, &block)
          idx = 0
          set_limit = options[:limit]
          while ((results = clone.limit(by).skip(idx)) && results.any?)
            results.each do |result|
              return self if set_limit and set_limit >= total
              yield result
              idx += 1
            end
          end
          self
        end
      end
    end

Of course you must be careful that the collection doesn’t change during the iteration. If you add or remove an item before you, or will skip elements or process some elements twice.

 

 

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}