module Mongo::Collection::View::Iterable

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

@since 2.0.0

Public Instance Methods

close_query() click to toggle source

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

@example Stop the iteration.

view.close_query

@since 2.1.0

# File lib/mongo/collection/view/iterable.rb, line 56
def close_query
  @cursor.send(:kill_cursors) if @cursor && !@cursor.closed?
end
each() { |doc| ... } click to toggle source

Iterate through documents returned by a query with this View.

@example Iterate through the result of the view.

view.each do |document|
  p document
end

@return [ Enumerator ] The enumerator.

@since 2.0.0

@yieldparam [ Hash ] Each matching document.

# File lib/mongo/collection/view/iterable.rb, line 37
def each
  @cursor = nil
  read_with_retry do
    server = read.select_server(cluster, false)
    result = send_initial_query(server)
    @cursor = Cursor.new(view, result, server)
  end
  @cursor.each do |doc|
    yield doc
  end if block_given?
  @cursor.to_enum
end

Private Instance Methods

initial_command_op() click to toggle source
# File lib/mongo/collection/view/iterable.rb, line 70
def initial_command_op
  if explained?
    Operation::Commands::Command.new(Builder::FindCommand.new(self).explain_specification)
  else
    Operation::Commands::Find.new(Builder::FindCommand.new(self).specification)
  end
end
initial_query_op(server) click to toggle source
# File lib/mongo/collection/view/iterable.rb, line 62
def initial_query_op(server)
  if server.features.find_command_enabled?
    initial_command_op
  else
    Operation::Read::Query.new(Builder::OpQuery.new(self).specification)
  end
end
send_initial_query(server) click to toggle source
# File lib/mongo/collection/view/iterable.rb, line 78
def send_initial_query(server)
  initial_query_op(server).execute(server.context)
end