Module: Mongo::Collection::View::Iterable

Included in:
Mongo::Collection::View, Aggregation
Defined in:
lib/mongo/collection/view/iterable.rb

Overview

Defines iteration related behaviour for collection views, including cursor instantiation.

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#close_queryObject Also known as: kill_cursors

Stop the iteration by sending a KillCursors command to the server.

Examples:

Stop the iteration.

view.close_query

Since:

  • 2.1.0



57
58
59
# File 'lib/mongo/collection/view/iterable.rb', line 57

def close_query
  @cursor.send(:kill_cursors) if @cursor && !@cursor.closed?
end

#each {|Each| ... } ⇒ Enumerator

Iterate through documents returned by a query with this View.

Examples:

Iterate through the result of the view.

view.each do |document|
  p document
end

Yield Parameters:

  • Each (Hash)

    matching document.

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 2.0.0



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mongo/collection/view/iterable.rb', line 37

def each
  @cursor = nil
  session = client.send(:get_session, @options)
  read_with_retry do
    server = server_selector.select_server(cluster, false)
    result = send_initial_query(server, session)
    @cursor = Cursor.new(view, result, server, session: session)
  end
  @cursor.each do |doc|
    yield doc
  end if block_given?
  @cursor.to_enum
end