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.
Instance Attribute Summary
Attributes included from Mongo::CursorHost
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.
Methods included from Mongo::CursorHost
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.
72 73 74 75 76 |
# File 'lib/mongo/collection/view/iterable.rb', line 72 def close_query return unless @cursor @cursor.close end |
#each {|Each| ... } ⇒ Enumerator
Iterate through documents returned by a query with this View.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mongo/collection/view/iterable.rb', line 41 def each(&block) @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 # TODO: rather than pulling the entire (limited) result set into # memory, this should tell the cursor about the limit and then let # the cursor limit the iteration as necessary. cursor_to_iterate = if limit_for_cached_query @cursor.to_a[0...limit_for_cached_query] else @cursor end cursor_to_iterate.each(&block) end |