lib/mongoid/criterion/scrollable.rb in mongoid-scroll-0.2.0 vs lib/mongoid/criterion/scrollable.rb in mongoid-scroll-0.2.1
- old
+ new
@@ -11,17 +11,17 @@
# introduce a default sort order if there's none
criteria = criteria.asc(:_id)
end
# scroll field and direction
scroll_field = criteria.options[:sort].keys.first
- scroll_direction = criteria.options[:sort].values.first.to_i == 1 ? '$gt' : '$lt'
+ scroll_direction = criteria.options[:sort].values.first.to_i
# scroll cursor from the parameter, with value and tiebreak_id
field = criteria.klass.fields[scroll_field.to_s]
cursor_options = { field_type: field.type, field_name: scroll_field, direction: scroll_direction }
cursor = cursor.is_a?(Mongoid::Scroll::Cursor) ? cursor : Mongoid::Scroll::Cursor.new(cursor, cursor_options)
# scroll
if block_given?
- criteria.where(cursor.criteria).each do |record|
+ criteria.where(cursor.criteria).order_by(_id: scroll_direction).each do |record|
yield record, Mongoid::Scroll::Cursor.from_record(record, cursor_options)
end
else
criteria
end