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

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

Overview

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

Since:

  • 2.0.0

Instance Attribute Summary

Attributes included from Mongo::CursorHost

#cursor, #timeout_mode

Instance Method Summary collapse

Methods included from Mongo::CursorHost

#validate_timeout_mode!

Instance Method Details

#close_querynil Also known as: kill_cursors

Note:

This method propagates any errors that occur when closing the server-side cursor.

Cleans up resources associated with this query.

If there is a server cursor associated with this query, it is closed by sending a KillCursors command to the server.

Returns:

  • (nil)

    Always nil.

Raises:

Since:

  • 2.1.0



75
76
77
78
79
# File 'lib/mongo/collection/view/iterable.rb', line 75

def close_query
  if @cursor
    @cursor.close
  end
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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongo/collection/view/iterable.rb', line 43

def each
  @cursor = prefer_cached_cursor? ? cached_cursor : new_cursor_for_iteration
  return @cursor.to_enum unless block_given?

  limit_for_cached_query = compute_limit_for_cached_query

  # Ruby versions 2.5 and older do not support arr[0..nil] syntax, so
  # this must be a separate conditional.
  cursor_to_iterate = if limit_for_cached_query
    @cursor.to_a[0...limit_for_cached_query]
  else
    @cursor
  end

  cursor_to_iterate.each do |doc|
    yield doc
  end
end