lib/mongo/collection/view/readable.rb in mongo-2.18.0.beta1 vs lib/mongo/collection/view/readable.rb in mongo-2.18.0

- old
+ new

@@ -56,10 +56,11 @@ # # @return [ Aggregation ] The aggregation object. # # @since 2.0.0 def aggregate(pipeline, options = {}) + options = @options.merge(options) unless Mongo.broken_view_options aggregation = Aggregation.new(self, pipeline, options) # Because the $merge and $out pipeline stages write documents to the # collection, it is necessary to clear the cache when they are performed. # @@ -165,10 +166,11 @@ # the following operators will need to be substituted when switching to #count_documents: # * $where should be replaced with $expr (only works on 3.6+) # * $near should be replaced with $geoWithin with $center # * $nearSphere should be replaced with $geoWithin with $centerSphere def count(opts = {}) + opts = @options.merge(opts) unless Mongo.broken_view_options cmd = { :count => collection.name, :query => filter } cmd[:skip] = opts[:skip] if opts[:skip] cmd[:hint] = opts[:hint] if opts[:hint] cmd[:limit] = opts[:limit] if opts[:limit] if read_concern @@ -217,16 +219,17 @@ # # @return [ Integer ] The document count. # # @since 2.6.0 def count_documents(opts = {}) + opts = @options.merge(opts) unless Mongo.broken_view_options pipeline = [:'$match' => filter] pipeline << { :'$skip' => opts[:skip] } if opts[:skip] pipeline << { :'$limit' => opts[:limit] } if opts[:limit] pipeline << { :'$group' => { _id: 1, n: { :'$sum' => 1 } } } - opts = opts.select { |k, _| [:hint, :max_time_ms, :read, :collation, :session, :comment].include?(k) } + opts = opts.slice(:hint, :max_time_ms, :read, :collation, :session, :comment) opts[:collation] ||= collation first = aggregate(pipeline, opts).first return 0 unless first first['n'].to_i @@ -252,15 +255,16 @@ unless view.filter.empty? raise ArgumentError, "Cannot call estimated_document_count when querying with a filter" end %i[limit skip].each do |opt| - if @options.key?(opt) + if options.key?(opt) || opts.key?(opt) raise ArgumentError, "Cannot call estimated_document_count when querying with #{opt}" end end + opts = @options.merge(opts) unless Mongo.broken_view_options Mongo::Lint.validate_underscore_read_preference(opts[:read]) read_pref = opts[:read] || read_preference selector = ServerSelector.get(read_pref || server_selector) with_session(opts) do |session| read_with_retry(session, selector) do |server| @@ -303,21 +307,24 @@ # # @option opts :max_time_ms [ Integer ] The maximum amount of time to allow the # command to run. # @option opts [ Hash ] :read The read preference options. # @option opts [ Hash ] :collation The collation to use. + # @option options [ Object ] :comment A user-provided + # comment to attach to this command. # # @return [ Array<Object> ] The list of distinct values. # # @since 2.0.0 def distinct(field_name, opts = {}) if field_name.nil? raise ArgumentError, 'Field name for distinct operation must be not nil' end + opts = @options.merge(opts) unless Mongo.broken_view_options cmd = { :distinct => collection.name, :key => field_name.to_s, - :query => filter } + :query => filter, } cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms] if read_concern cmd[:readConcern] = Options::Mapper.transform_values_to_strings( read_concern) end @@ -330,9 +337,10 @@ selector: cmd, db_name: database.name, options: {:limit => -1}, read: read_pref, session: session, + comment: opts[:comment], # For some reason collation was historically accepted as a # string key. Note that this isn't documented as valid usage. collation: opts[:collation] || opts['collation'] || collation, ).execute(server, context: Operation::Context.new(client: client, session: session)) end.first['values']