Sha256: 445693ff8df85a2f18aa159bcbc3f85fac07b4911fa467d00011f46cafa3303d

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'bundler'
Bundler.setup(:default, :development)

require 'mongoid-scroll'
require 'faker'

fail 'No Mongo Ruby Driver' unless Object.const_defined?(:Mongo)

Mongo::Logger.logger = Logger.new($stdout)
Mongo::Logger.logger.level = Logger::INFO

Mongoid.connect_to 'mongoid_scroll_demo'
Mongoid.purge!

# total items to insert
total_items = 20
# a MongoDB query will be executed every scroll_by items
scroll_by = 7

# insert items with a position out-of-order
rands = (0..total_items).to_a.sort { rand }[0..total_items]
total_items.times do
  Mongoid.default_client['feed_items'].insert_one(title: Faker::Lorem.sentence, position: rands.pop)
end

Mongoid.default_client['feed_items'].indexes.create_one(position: 1, _id: 1)

total_shown = 0
next_cursor = nil
loop do
  current_cursor = next_cursor
  next_cursor = nil
  Mongoid.default_client['feed_items'].find.limit(scroll_by).sort(position: 1).scroll(current_cursor, field_type: Integer, field_name: 'position') do |item, cursor|
    puts "#{item['position']}: #{item['title']}"
    next_cursor = cursor
    total_shown += 1
  end
  break unless next_cursor
  # destroy an item just for the heck of it, scroll is not affected
  item = Mongoid.default_client['feed_items'].find.sort(position: 1).first
  Mongoid.default_client['feed_items'].find(_id: item['_id']).delete_one
end

# this will be 20
puts "Shown #{total_shown} items."

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-scroll-0.3.4 examples/mongo_ruby_driver_scroll_feed.rb