Method: Mongo::Collection::View::Readable#count_documents
- Defined in:
- lib/mongo/collection/view/readable.rb
#count_documents(opts = {}) ⇒ Integer
Get a count of matching documents in the collection.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/mongo/collection/view/readable.rb', line 223 def count_documents(opts = {}) opts = .merge(opts) unless Mongo. 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.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 end |