Module: Mongoid::QueryCache::View

Extended by:
ActiveSupport::Concern
Defined in:
build/mongoid-7.0/lib/mongoid/query_cache.rb

Overview

Contains enhancements to the Mongo::Collection::View in order to get a cached cursor or a regular cursor on iteration.

Since:

  • 5.0.0

Instance Method Summary collapse

Instance Method Details

#eachObject

Override the default enumeration to handle if the cursor can be cached or not.

Examples:

Iterate over the view.

view.each do |doc|
  # ...
end

Since:

  • 5.0.0



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'build/mongoid-7.0/lib/mongoid/query_cache.rb', line 225

def each
  if system_collection? || !QueryCache.enabled? || (respond_to?(:write?, true) && write?)
    super
  else
    @cursor = nil
    unless @cursor = cached_cursor

      if driver_supports_cursor_sessions?
        session = client.send(:get_session, @options)
        read_with_retry(session, server_selector) do |server|
          result = send_initial_query(server, session)
          @cursor = get_cursor(result, server, session)
        end
      else
        read_with_retry do
          server = server_selector.select_server(cluster)
          result = send_initial_query(server)
          @cursor = get_cursor(result, server)
        end
      end
    end

    if block_given?
      if limit && limit != -1
        @cursor.to_a[0...limit].each do |doc|
          yield doc
        end
      else
        @cursor.each do |doc|
          yield doc
        end
      end
    else
      @cursor.to_enum
    end
  end
end