# File lib/mongo/db.rb, line 518
    def command(selector, opts={})
      check_response = opts.fetch(:check_response, true)
      socket         = opts[:socket]
      raise MongoArgumentError, "command must be given a selector" unless selector.is_a?(Hash) && !selector.empty?
      if selector.keys.length > 1 && RUBY_VERSION < '1.9' && selector.class != BSON::OrderedHash
        raise MongoArgumentError, "DB#command requires an OrderedHash when hash contains multiple keys"
      end

      if read_pref = opts[:read]
        Mongo::ReadPreference::validate(read_pref)
        unless read_pref == :primary || Mongo::Support::secondary_ok?(selector)
          raise MongoArgumentError, "Command is not supported on secondaries: #{selector.keys.first}"
        end
      end

      begin
        result = Cursor.new(system_command_collection,
                            :limit => -1,
                            :selector => selector,
                            :socket => socket,
                            :read => read_pref,
                            :comment => opts[:comment]).next_document
      rescue OperationFailure => ex
        raise OperationFailure, "Database command '#{selector.keys.first}' failed: #{ex.message}"
      end

      if result.nil?
        raise OperationFailure, "Database command '#{selector.keys.first}' failed: returned null."
      elsif (check_response && !ok?(result))
        message = "Database command '#{selector.keys.first}' failed: ("
        message << result.map do |key, value|
          "#{key}: '#{value}'"
        end.join('; ')
        message << ').'
        code = result['code'] || result['assertionCode']
        raise OperationFailure.new(message, code, result)
      else
        result
      end
    end