# File lib/mongo/collection.rb, line 660
    def aggregate(pipeline=nil, opts={})
      raise MongoArgumentError, "pipeline must be an array of operators" unless pipeline.class == Array
      raise MongoArgumentError, "pipeline operators must be hashes" unless pipeline.all? { |op| op.class == Hash }

      hash = BSON::OrderedHash.new
      hash['aggregate'] = self.name
      hash['pipeline'] = pipeline

      result = @db.command(hash, command_options(opts))
      unless Mongo::Support.ok?(result)
        raise Mongo::OperationFailure, "aggregate failed: #{result['errmsg']}"
      end

      return result["result"]
    end