# File lib/mongo/collection/view/readable.rb, line 180
        def count_documents(opts = {})
          pipeline = ['$match''$match' => filter]
          pipeline << { '$skip''$skip' => opts[:skip] } if opts[:skip]
          pipeline << { '$limit''$limit' => opts[:limit] } if opts[:limit]
          pipeline << { '$group''$group' => { _id: nil, n: { '$sum''$sum' => 1 } } }

          opts.select! { |k, _| [:hint, :max_time_ms, :read, :collation, :session].include?(k) }
          first = aggregate(pipeline, opts).first
          return 0 unless first
          first['n'].to_i
        end