diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 4179c2c..2d31add 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -93,6 +93,18 @@ module Mongo end end + # Guess whether the cursor is alive on the server. + # + # Note that this method only checks whether we have + # a cursor id. The cursor may still have timed out + # on the server. This will be indicated in the next + # call to Cursor#next_document. + # + # @return [Boolean] + def alive? + @cursor_id && @cursor_id != 0 + end + # Get the next document specified the cursor options. # # @return [Hash, Nil] the next document or Nil if no documents remain. diff --git a/test/cursor_test.rb b/test/cursor_test.rb index b06b58d..8ed4989 100644 --- a/test/cursor_test.rb +++ b/test/cursor_test.rb @@ -16,6 +16,22 @@ class CursorTest < Test::Unit::TestCase @@coll_full_name = "#{MONGO_TEST_DB}.test" end + def test_alive + batch = [] + 5000.times do |n| + batch << {:a => n} + end + + @@coll.insert(batch) + cursor = @@coll.find + assert !cursor.alive? + cursor.next + assert cursor.alive? + cursor.close + assert !cursor.alive? + @@coll.remove + end + def test_add_options c = @@coll.find c.add_option(OP_QUERY_EXHAUST)