Skip to content

Commit 70c23e2

Browse files
author
Mike Dirolf
committed
deprecate :offset option to find in favor of :skip
1 parent 2b70111 commit 70c23e2

File tree

7 files changed

+32
-11
lines changed

7 files changed

+32
-11
lines changed

examples/queries.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
# records we find.
3939
coll.find('a' => 1)
4040

41-
# Find records sort by 'a', offset 1, limit 2 records.
41+
# Find records sort by 'a', skip 1, limit 2 records.
4242
# Sort can be single name, array, or hash.
43-
coll.find({}, {:offset => 1, :limit => 2, :sort => 'a'})
43+
coll.find({}, {:skip => 1, :limit => 2, :sort => 'a'})
4444

4545
# Find all records with 'a' > 1. There is also $lt, $gte, and $lte.
4646
coll.find({'a' => {'$gt' => 1}})

lib/mongo/collection.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def hint=(hint)
8888
# set ("_id" will always be included). By limiting results
8989
# to a certain subset of fields you can cut down on network
9090
# traffic and decoding time.
91-
# :offset :: Start at this record when returning records
91+
# :skip :: Number of documents to omit (from the start of the result set)
92+
# when returning the results
9293
# :limit :: Maximum number of records to return
9394
# :sort :: Either hash of field names as keys and 1/-1 as values; 1 ==
9495
# ascending, -1 == descending, or array of field names (all
@@ -102,7 +103,11 @@ def hint=(hint)
102103
def find(selector={}, options={})
103104
fields = options.delete(:fields)
104105
fields = ["_id"] if fields && fields.empty?
105-
offset = options.delete(:offset) || 0
106+
skip = options.delete(:offset) || nil
107+
if !skip.nil?
108+
warn "the :offset option to find is deprecated and will be removed. please use :skip instead"
109+
end
110+
skip = options.delete(:skip) || skip || 0
106111
limit = options.delete(:limit) || 0
107112
sort = options.delete(:sort)
108113
hint = options.delete(:hint)
@@ -114,7 +119,7 @@ def find(selector={}, options={})
114119
end
115120
raise RuntimeError, "Unknown options [#{options.inspect}]" unless options.empty?
116121

117-
cursor = @db.query(self, Query.new(selector, fields, offset, limit, sort, hint, snapshot))
122+
cursor = @db.query(self, Query.new(selector, fields, skip, limit, sort, hint, snapshot))
118123
if block_given?
119124
yield cursor
120125
cursor.close()

lib/mongo/cursor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def limit(number_to_return)
107107
#
108108
# Raises InvalidOperation if this cursor has already been used.
109109
#
110-
# This method overrides any offset specified in the Collection#find method,
110+
# This method overrides any skip specified in the Collection#find method,
111111
# and only the last skip applied has an effect.
112112
def skip(number_to_skip)
113113
check_modifiable

lib/mongo/query.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class Query
3838
# (Called :fields in calls to Collection#find.)
3939
#
4040
# number_to_skip :: Number of records to skip before returning
41-
# records. (Called :offset in calls to
42-
# Collection#find.) Default is 0.
41+
# records. Default is 0.
4342
#
4443
# number_to_return :: Max number of records to return. (Called :limit
4544
# in calls to Collection#find.) Default is 0 (all

test/mongo-qa/find1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ if $DEBUG
1111
(5..15).each { |i| c.insert(:x => 2, :y => i, :z => (i+64).chr) }
1212
end
1313

14-
cursor = db.collection('c').find({'x' => 1}, :sort => 'y', :offset => 20, :limit => 10)
14+
cursor = db.collection('c').find({'x' => 1}, :sort => 'y', :skip => 20, :limit => 10)
1515
cursor.each { |row| puts row['z'] }

test/test_collection.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_optional_find_block
165165
assert_equal 1, x
166166

167167
i = 0
168-
@@test.find({}, :offset => 5) do |cursor|
168+
@@test.find({}, :skip => 5) do |cursor|
169169
cursor.each do |doc|
170170
i = i + 1
171171
end
@@ -199,6 +199,23 @@ def test_save_symbol_find_string
199199
# assert_equal :mike, @@test.find_one("foo" => "mike")["foo"]
200200
end
201201

202+
def test_limit_and_skip
203+
10.times do |i|
204+
@@test.save(:foo => i)
205+
end
206+
207+
# TODO remove test for deprecated :offset option
208+
assert_equal 5, @@test.find({}, :offset => 5).next_object()["foo"]
209+
210+
assert_equal 5, @@test.find({}, :skip => 5).next_object()["foo"]
211+
assert_equal nil, @@test.find({}, :skip => 10).next_object()
212+
213+
assert_equal 5, @@test.find({}, :limit => 5).to_a.length
214+
215+
assert_equal 3, @@test.find({}, :skip => 3, :limit => 5).next_object()["foo"]
216+
assert_equal 5, @@test.find({}, :skip => 3, :limit => 5).to_a.length
217+
end
218+
202219
def test_group_with_scope
203220
@@test.save("a" => 1)
204221
@@test.save("b" => 1)

test/test_cursor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_count
4343
assert_equal 10, @@coll.find().count()
4444
assert_kind_of Integer, @@coll.find().count()
4545
assert_equal 10, @@coll.find({}, :limit => 5).count()
46-
assert_equal 10, @@coll.find({}, :offset => 5).count()
46+
assert_equal 10, @@coll.find({}, :skip => 5).count()
4747

4848
assert_equal 1, @@coll.find({"x" => 1}).count()
4949
assert_equal 5, @@coll.find({"x" => {"$lt" => 5}}).count()

0 commit comments

Comments
 (0)