lib/mongo/collection/view/readable.rb in mongo-2.2.7 vs lib/mongo/collection/view/readable.rb in mongo-2.3.0
- old
+ new
@@ -1,6 +1,6 @@
-# Copyright (C) 2014-2015 MongoDB, Inc.
+# Copyright (C) 2014-2016 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
@@ -118,10 +118,11 @@
# @option options :hint [ Hash ] Override default index selection and force
# MongoDB to use a specific index for the query.
# @option options :limit [ Integer ] Max number of docs to return.
# @option options :max_time_ms [ Integer ] The maximum amount of time to allow the
# command to run.
+ # @option options [ Hash ] :read The read preference options.
#
# @return [ Integer ] The document count.
#
# @since 2.0.0
def count(options = {})
@@ -130,10 +131,11 @@
cmd[:hint] = options[:hint] if options[:hint]
cmd[:limit] = options[:limit] if options[:limit]
cmd[:maxTimeMS] = options[:max_time_ms] if options[:max_time_ms]
cmd[:readConcern] = collection.read_concern if collection.read_concern
read_with_retry do
+ options = options.merge(read: read) unless options[:read]
database.command(cmd, options).n.to_i
end
end
# Get a list of distinct values for a specific field.
@@ -144,11 +146,11 @@
# @param [ String, Symbol ] field_name The name of the field.
# @param [ Hash ] options Options for the distinct command.
#
# @option options :max_time_ms [ Integer ] The maximum amount of time to allow the
# command to run.
- # @option options :read [ Hash ] The read preference for this command.
+ # @option options [ Hash ] :read The read preference options.
#
# @return [ Array<Object> ] The list of distinct values.
#
# @since 2.0.0
def distinct(field_name, options={})
@@ -156,10 +158,11 @@
:key => field_name.to_s,
:query => filter }
cmd[:maxTimeMS] = options[:max_time_ms] if options[:max_time_ms]
cmd[:readConcern] = collection.read_concern if collection.read_concern
read_with_retry do
+ options = options.merge(read: read) unless options[:read]
database.command(cmd, options).first['values']
end
end
# The index that MongoDB will be forced to use for the query.
@@ -291,11 +294,11 @@
# new +View+.
#
# @since 2.0.0
def read(value = nil)
return default_read if value.nil?
- selector = value.is_a?(Hash) ? ServerSelector.get(client.options.merge(value)) : value
+ selector = ServerSelector.get(value)
configure(:read, selector)
end
# Set whether to return only the indexed field or fields.
#
@@ -414,10 +417,24 @@
# @since 2.1.0
def max_time_ms(max = nil)
configure(:max_time_ms, max)
end
+ # The type of cursor to use. Can be :tailable or :tailable_await.
+ #
+ # @example Set the cursor type.
+ # view.cursor_type(:tailable)
+ #
+ # @param [ :tailable, :tailable_await ] type The cursor type.
+ #
+ # @return [ :tailable, :tailable_await, View ] Either the cursor type setting or a new +View+.
+ #
+ # @since 2.3.0
+ def cursor_type(type = nil)
+ configure(:cursor_type, type)
+ end
+
private
def default_read
options[:read] || read_preference
end
@@ -427,22 +444,22 @@
Operation::Commands::ParallelScan.new(
:coll_name => collection.name,
:db_name => database.name,
:cursor_count => cursor_count,
:read_concern => collection.read_concern
- ).execute(server.context).cursor_ids.map do |cursor_id|
+ ).execute(server).cursor_ids.map do |cursor_id|
result = if server.features.find_command_enabled?
Operation::Commands::GetMore.new({
:selector => { :getMore => cursor_id, :collection => collection.name },
:db_name => database.name
- }).execute(server.context)
+ }).execute(server)
else
Operation::Read::GetMore.new({
:to_return => 0,
:cursor_id => cursor_id,
:db_name => database.name,
:coll_name => collection.name
- }).execute(server.context)
+ }).execute(server)
end
Cursor.new(self, result, server)
end
end