# AspireMongoDBDriverExtensions Methods

- Package: [Aspire.MongoDB.Driver](/reference/api/csharp/aspire.mongodb.driver.md)
- Type: [AspireMongoDBDriverExtensions](/reference/api/csharp/aspire.mongodb.driver/aspiremongodbdriverextensions.md)
- Kind: `Methods`
- Members: `2`

Extension methods for connecting MongoDB database with MongoDB.Driver client.

## AddKeyedMongoDBClient(IHostApplicationBuilder, string, Action<MongoDBSettings>, Action<MongoClientSettings>)

- Name: `AddKeyedMongoDBClient(IHostApplicationBuilder, string, Action<MongoDBSettings>, Action<MongoClientSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs#L57-L65)

Registers `Driver.IMongoClient` and `Driver.IMongoDatabase` instances for connecting MongoDB database with MongoDB.Driver client.

```csharp
public static class AspireMongoDBDriverExtensions
{
    public static void AddKeyedMongoDBClient(
        this IHostApplicationBuilder builder,
        string name,
        Action<MongoDBSettings>? configureSettings = null,
        Action<MongoClientSettings>? configureClientSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `name` (`string`)
  The name of the component, which is used as the `ServiceDescriptor.ServiceKey` of the service and also to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MongoDBSettings>`) `optional`
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.
- `configureClientSettings` (`Action<MongoClientSettings>`) `optional`
  An optional delegate that can be used for customizing MongoClientSettings.

## Exceptions

- `ArgumentNullException` -- Thrown if mandatory `builder` is null.
- `InvalidOperationException` -- Thrown when mandatory [MongoDBSettings.ConnectionString](/reference/api/csharp/aspire.mongodb.driver/mongodbsettings/properties.md#connectionstring) is not provided.

## Remarks

Reads the configuration from "Aspire:MongoDB:Driver:{name}" section.

## AddMongoDBClient(IHostApplicationBuilder, string, Action<MongoDBSettings>, Action<MongoClientSettings>)

- Name: `AddMongoDBClient(IHostApplicationBuilder, string, Action<MongoDBSettings>, Action<MongoClientSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.MongoDB.Driver/AspireMongoDBDriverExtensions.cs#L38-L39)

Registers `Driver.IMongoClient` and `Driver.IMongoDatabase` instances for connecting MongoDB database with MongoDB.Driver client.

```csharp
public static class AspireMongoDBDriverExtensions
{
    public static void AddMongoDBClient(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<MongoDBSettings>? configureSettings = null,
        Action<MongoClientSettings>? configureClientSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  A name used to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MongoDBSettings>`) `optional`
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.
- `configureClientSettings` (`Action<MongoClientSettings>`) `optional`
  An optional delegate that can be used for customizing MongoClientSettings.

## Exceptions

- `InvalidOperationException` -- Thrown when mandatory [MongoDBSettings.ConnectionString](/reference/api/csharp/aspire.mongodb.driver/mongodbsettings/properties.md#connectionstring) is not provided.

## Remarks

Reads the configuration from "Aspire:MongoDB:Driver" section.
