Module: Mongo::Collection::View::Iterable
- Included in:
- Mongo::Collection::View, Aggregation
- Defined in:
- build/ruby-driver-v2.19/lib/mongo/collection/view/iterable.rb
Overview
Defines iteration related behavior for collection views, including cursor instantiation.
Instance Attribute Summary collapse
-
#cursor ⇒ nil | Cursor
readonly
private
Returns the cursor associated with this view, if any.
Instance Method Summary collapse
-
#close_query ⇒ nil
(also: #kill_cursors)
Cleans up resources associated with this query.
-
#each {|Each| ... } ⇒ Enumerator
Iterate through documents returned by a query with this
View
.
Instance Attribute Details
#cursor ⇒ nil | 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.
33 34 35 |
# File 'build/ruby-driver-v2.19/lib/mongo/collection/view/iterable.rb', line 33 def cursor @cursor end |
Instance Method Details
#close_query ⇒ nil 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.
106 107 108 109 110 |
# File 'build/ruby-driver-v2.19/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
.
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 'build/ruby-driver-v2.19/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, **) 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 |