Method: Mongo::Cursor#count

Defined in:
lib/mongo/cursor.rb

#count(skip_and_limit = false) ⇒ Integer

Get the size of the result set for this query.

Parameters:

  • whether (Boolean)

    of not to take notice of skip and limit

Returns:

  • (Integer)

    the number of objects in the result set for this query.

Raises:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mongo/cursor.rb', line 119

def count(skip_and_limit = false)
  command = BSON::OrderedHash["count",  @collection.name, "query",  @selector]

  if skip_and_limit
    command.merge!(BSON::OrderedHash["limit", @limit]) if @limit != 0
    command.merge!(BSON::OrderedHash["skip", @skip]) if @skip != 0
  end

  command.merge!(BSON::OrderedHash["fields", @fields])

  response = @db.command(command)
  return response['n'].to_i if Mongo::Support.ok?(response)
  return 0 if response['errmsg'] == "ns missing"
  raise OperationFailure, "Count failed: #{response['errmsg']}"
end