lib/mongo/collection/view/readable.rb
# Gets an estimate of the count of documents in a collection using collection metadata.
#
# @example Get the number of documents in the collection.
#   collection_view.estimated_document_count
#
# @param [ Hash ] opts Options for the operation.
#
# @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.
#
# @return [ Integer ] The document count.
#
# @since 2.6.0
Gets an estimate of the count of documents in a collection using collection metadata.
Gets an estimate of the count of documents in a collection using collection metadata .
def estimated_document_count(opts = {})
          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)
              raise ArgumentError, "Cannot call estimated_document_count when querying with #{opt}"
            end
          end
          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|
              context = Operation::Context.new(client: client, session: session)
              if server.description.server_version_gte?('5.0')
                pipeline = [
                  {'$collStats' => {'count' => {}}},
                  {'$group' => {'_id' => 1, 'n' => {'$sum' => '$count'}}},
                ]
                spec = Builder::Aggregation.new(pipeline, self, options.merge(session: session)).specification
                result = Operation::Aggregate.new(spec).execute(server, context: context)
                result.documents.first.fetch('n')
              else
                cmd = { count: collection.name }
                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
                result = Operation::Count.new(
                  selector: cmd,
                  db_name: database.name,
                  read: read_pref,
                  session: session,
                ).execute(server, context: context)
                result.n.to_i
              end
            end
          end
        rescue Error::OperationFailure => exc
          if exc.code == 26
            0
          else
            raise
          end
        end
def estimated_document_count ( opts = { } ) 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 ) raise ArgumentError , "Cannot call estimated_document_count when querying with #{opt}" end end 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 | context = Operation :: Context . new ( client : client , session : session ) if server . description . server_version_gte? ( '5.0' ) pipeline = [ { '$collStats' => { 'count' => { } } } , { '$group' => { '_id' => 1 , 'n' => { '$sum' => '$count' } } } , ] spec = Builder :: Aggregation . new ( pipeline , self , options . merge ( session : session ) ) . specification result = Operation :: Aggregate . new ( spec ) . execute ( server , context : context ) result . documents . first . fetch ( 'n' ) else cmd = { count : collection . name } 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 result = Operation :: Count . new ( selector : cmd , db_name : database . name , read : read_pref , session : session , ) . execute ( server , context : context ) result . n . to_i end end end rescue Error :: OperationFailure => exc if exc . code == 26 0 else raise end end
Gets an estimate of the count of documents in a collection using collection metadata.
Gets an estimate of the count of documents in a collection using collection metadata .
# NamespaceNotFound # This should only happen with the aggregation pipeline path # (server 4.9+). Previous servers should return 0 for nonexistent # collections.
The document count.
The document count .
 Integer 
opts
:read The read preference options.
: read The read preference options .
<not_specific>
opts Options for the operation.
opts Options for the operation .
example
Get the number of documents in the collection.
Get the number of documents in the collection .
since
None