Class: Mongo::Cursor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/mongo/cursor.rb

Overview

Note:

The Cursor API is semipublic.

Client-side representation of an iterator over a query result set on the server.

A Cursor is not created directly by a user. Rather, CollectionView creates a Cursor in an Enumerable module method.

Examples:

Get an array of 5 users named Emily.

users.find({:name => 'Emily'}).limit(5).to_a

Call a block on each user doc.

users.find.each { |doc| puts doc }

Instance Method Summary (collapse)

Constructor Details

- (Cursor) initialize(view, result, server)

Creates a Cursor object.

Examples:

Instantiate the cursor.

Mongo::Cursor.new(view, response, server)

Parameters:

  • view (CollectionView)

    The CollectionView defining the query.

  • result (Operation::Result)

    The result of the first execution.

  • server (Server)

    The server this cursor is locked to.

Since:

  • 2.0.0



48
49
50
51
52
53
# File 'lib/mongo/cursor.rb', line 48

def initialize(view, result, server)
  @view = view
  @server = server
  @initial_result = result
  @remaining = limit if limited?
end

Instance Method Details

- (Enumerator) each

Iterate through documents returned from the query.

Examples:

Iterate over the documents in the cursor.

cursor.each do |doc|
  ...
end

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 2.0.0



77
78
79
80
81
82
83
# File 'lib/mongo/cursor.rb', line 77

def each
  process(@initial_result).each { |doc| yield doc }
  while more?
    return kill_cursors if exhausted?
    get_more.each { |doc| yield doc }
  end
end

- (String) inspect

Get a human-readable string representation of Cursor.

Examples:

Inspect the cursor.

cursor.inspect

Returns:

  • (String)

    A string representation of a Cursor instance.

Since:

  • 2.0.0



63
64
65
# File 'lib/mongo/cursor.rb', line 63

def inspect
  "#<Mongo::Cursor:0x#{object_id} @view=#{@view.inspect}>"
end