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

Included in:
Mongo::Collection::View, Aggregation
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 collapse

Instance Method Summary collapse

Instance Attribute Details

#cursornil | Cursor (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the cursor associated with this view, if any.

Returns:

  • (nil | Cursor)

    The cursor, if any.

Since:

  • 2.0.0



33
34
35
# File 'lib/mongo/collection/view/iterable.rb', line 33

def cursor
  @cursor
end

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



106
107
108
109
110
# File 'lib/mongo/collection/view/iterable.rb', line 106

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mongo/collection/view/iterable.rb', line 47

def each
  # If the caching cursor is closed and was not fully iterated,
  # the documents we have in it are not the complete result set and
  # we have no way of completing that iteration.
  # Therefore, discard that cursor and start iteration again.
  # The case of the caching cursor not being closed and not having
  # been fully iterated isn't tested - see RUBY-2773.
  @cursor = if use_query_cache? && cached_cursor && (
    cached_cursor.fully_iterated? || !cached_cursor.closed?
  )
    cached_cursor
  else
    session = client.send(:get_session, @options)
    select_cursor(session).tap do |cursor|
      if use_query_cache?
        # No need to store the cursor in the query cache if there is
        # already a cached cursor stored at this key.
        QueryCache.set(cursor, **cache_options)
      end
    end
  end

  if use_query_cache?
    # If a query with a limit is performed, the query cache will
    # re-use results from an earlier query with the same or larger
    # limit, and then impose the lower limit during iteration.
    limit_for_cached_query = respond_to?(:limit) ? QueryCache.normalized_limit(limit) : nil
  end

  if block_given?
    # 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
  else
    @cursor.to_enum
  end
end