Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Tutorial on Paging and Iterating Over Large MongoDB Collection

DZone's Guide to

A Tutorial on Paging and Iterating Over Large MongoDB Collection

· Database Zone ·
Free Resource

Databases are better when they can run themselves. CockroachDB is a SQL database that automates scaling and recovery. Check it out here.

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.

 

 

Databases should be easy to deploy, easy to use, and easy to scale. If you agree, you should check out CockroachDB, a scalable SQL database built for businesses of every size. Check it out here. 

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}