Over a million developers have joined DZone.

A Tutorial on Paging and Iterating Over Large MongoDB Collection

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

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.

 

 

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of Daniel Doubrovkine. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}