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. Requires server version 3.6+.
# @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.
#
# @return [ Integer ] The document count.
#
# @since 2.6.0
Get a count of matching documents in the collection.
Get a count of matching documents in the collection .
def count_documents(opts = {})
          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].include?(k) }
          opts[:collation] ||= collation
          first = aggregate(pipeline, opts).first
          return 0 unless first
          first['n'].to_i
        end
def count_documents ( opts = { } ) 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 ] . include? ( k ) } opts [ :collation ] ||= collation first = aggregate ( pipeline , opts ) . first return 0 unless first first [ 'n' ] . to_i end
Get a count of matching documents in the collection.
Get a count of matching documents in the collection .
The document count.
The document count .
 Integer 
opts
:collation The collation to use.
: collation The collation to use .
<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