lib/mongo/collection/view/readable.rb
# Get a count of matching documents in the collection.
#
# @example Get the number of documents in the collection.
#   collection_view.count
#
# @param [ Hash ] opts Options for the operation.
#
# @option opts :skip [ Integer ] The number of documents to skip.
# @option opts :hint [ Hash ] Override default index selection and force
#   MongoDB to use a specific index for the query.
# @option opts :limit [ Integer ] Max number of docs to count.
# @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 opts [ Mongo::Session ] :session The session to use for the operation.
#
# @return [ Integer ] The document count.
#
# @since 2.0.0
#
# @deprecated Use #count_documents or #estimated_document_count instead. However, note that
#   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
Get a count of matching documents in the collection.
Get a count of matching documents in the collection .
def count(opts = {})
          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
            cmd[:readConcern] = Options::Mapper.transform_values_to_strings(
              read_concern)
          end
          cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms]
          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|
              Operation::Count.new(
                selector: cmd,
                db_name: database.name,
                options: {:limit => -1},
                read: read_pref,
                session: session,
                collation: opts[:collation] || opts['collation'] || collation,
              ).execute(server, context: Operation::Context.new(client: client, session: session))
            end.n.to_i
          end
        end
def count ( opts = { } ) 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 cmd [ :readConcern ] = Options :: Mapper . transform_values_to_strings ( read_concern ) end cmd [ :maxTimeMS ] = opts [ :max_time_ms ] if opts [ :max_time_ms ] 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 | Operation :: Count . new ( selector : cmd , db_name : database . name , options : { :limit => - 1 } , read : read_pref , session : session , collation : opts [ :collation ] || opts [ 'collation' ] || collation , ) . execute ( server , context : Operation :: Context . new ( client : client , session : session ) ) end . n . to_i end end
Get a count of matching documents in the collection.
Get a count of matching documents in the collection .
# For some reason collation was historically accepted as a # string key. Note that this isn't documented as valid usage.
The document count.
The document count .
 Integer 
opts
:session The session to use for the operation.
: session The session to use for the operation .
<not_specific>
opts Options for the operation.
opts Options for the operation .
example
Get the number of documents in the collection. collection_view.count
Get the number of documents in the collection . collection_view . count
since
None
deprecated
Use #count_documents or #estimated_document_count instead. However, note that 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
Use #count_documents or #estimated_document_count instead . However note that 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