# MongoDBBuilderExtensions Methods

- Package: [Aspire.Hosting.MongoDB](/reference/api/csharp/aspire.hosting.mongodb.md)
- Type: [MongoDBBuilderExtensions](/reference/api/csharp/aspire.hosting.mongodb/mongodbbuilderextensions.md)
- Kind: `Methods`
- Members: `9`

Provides extension methods for adding MongoDB resources to an `Hosting.IDistributedApplicationBuilder`.

## AddDatabase(IResourceBuilder<MongoDBServerResource>, string, string?)

- Name: `AddDatabase(IResourceBuilder<MongoDBServerResource>, string, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MongoDBDatabaseResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L109-L142)

Adds a MongoDB database to the application model.

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(
        this IResourceBuilder<MongoDBServerResource> builder,
        string name,
        string? databaseName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MongoDBServerResource>`)
  The MongoDB server resource builder.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.
- `databaseName` (`string?`) `optional`
  The name of the database. If not provided, this defaults to the same value as `name`.

## Returns

`IResourceBuilder<MongoDBDatabaseResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddMongoDB(IDistributedApplicationBuilder, string, int?)

- Name: `AddMongoDB(IDistributedApplicationBuilder, string, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MongoDBServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L37)

Adds a MongoDB resource to the application model. A container is used for local development.

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<MongoDBServerResource> AddMongoDB(
        this IDistributedApplicationBuilder builder,
        string name,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.
- `port` (`int?`)
  The host port for MongoDB.

## Returns

`IResourceBuilder<MongoDBServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This version of the package defaults to the tag of the container image.

This overload is not available in polyglot app hosts. Use [MongoDBBuilderExtensions.AddMongoDB(IDistributedApplicationBuilder, string, int?)](/reference/api/csharp/aspire.hosting.mongodb/mongodbbuilderextensions/methods.md#addmongodb-idistributedapplicationbuilder-string-int) instead.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## AddMongoDB(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)

- Name: `AddMongoDB(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MongoDBServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L58-L95)

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<MongoDBServerResource> AddMongoDB(
        this IDistributedApplicationBuilder builder,
        string name,
        int? port = null,
        IResourceBuilder<ParameterResource>? userName = null,
        IResourceBuilder<ParameterResource>? password = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.
- `port` (`int?`) `optional`
  The host port for MongoDB.
- `userName` (`IResourceBuilder<ParameterResource>`) `optional`
  A parameter that contains the MongoDb server user name, or `null` to use a default value.
- `password` (`IResourceBuilder<ParameterResource>`) `optional`
  A parameter that contains the MongoDb server password, or `null` to use a generated password.

## Returns

`IResourceBuilder<MongoDBServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataBindMount(IResourceBuilder<MongoDBServerResource>, string, bool)

- Name: `WithDataBindMount(IResourceBuilder<MongoDBServerResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MongoDBServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L223-L226)

Adds a bind mount for the data folder to a MongoDB container resource.

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<MongoDBServerResource> WithDataBindMount(
        this IResourceBuilder<MongoDBServerResource> builder,
        string source,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MongoDBServerResource>`)
  The resource builder.
- `source` (`string`)
  The source directory on the host to mount into the container.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only mount.

## Returns

`IResourceBuilder<MongoDBServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataVolume(IResourceBuilder<MongoDBServerResource>, string?, bool)

- Name: `WithDataVolume(IResourceBuilder<MongoDBServerResource>, string?, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MongoDBServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L207-L209)

Adds a named volume for the data folder to a MongoDB container resource.

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<MongoDBServerResource> WithDataVolume(
        this IResourceBuilder<MongoDBServerResource> builder,
        string? name = null,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MongoDBServerResource>`)
  The resource builder.
- `name` (`string?`) `optional`
  The name of the volume. Defaults to an auto-generated name based on the application and resource names.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only volume.

## Returns

`IResourceBuilder<MongoDBServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithHostPort(IResourceBuilder<MongoExpressContainerResource>, int?)

- Name: `WithHostPort(IResourceBuilder<MongoExpressContainerResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MongoExpressContainerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L188-L193)

Configures the host port that the Mongo Express resource is exposed on instead of using randomly assigned port.

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<MongoExpressContainerResource> WithHostPort(
        this IResourceBuilder<MongoExpressContainerResource> builder,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MongoExpressContainerResource>`)
  The resource builder for Mongo Express.
- `port` (`int?`)
  The port to bind on the host. If `null` is used random port will be assigned.

## Returns

`IResourceBuilder<MongoExpressContainerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithInitBindMount(IResourceBuilder<MongoDBServerResource>, string, bool)

> **Obsolete:** Use WithInitFiles instead.

- Name: `WithInitBindMount(IResourceBuilder<MongoDBServerResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MongoDBServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L241-L244)

Adds a bind mount for the init folder to a MongoDB container resource.

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<MongoDBServerResource> WithInitBindMount(
        this IResourceBuilder<MongoDBServerResource> builder,
        string source,
        bool isReadOnly = true)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MongoDBServerResource>`)
  The resource builder.
- `source` (`string`)
  The source directory on the host to mount into the container.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only mount.

## Returns

`IResourceBuilder<MongoDBServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

This method is not available in polyglot app hosts. Use [MongoDBBuilderExtensions.WithInitFiles(IResourceBuilder<MongoDBServerResource>, string)](/reference/api/csharp/aspire.hosting.mongodb/mongodbbuilderextensions/methods.md#withinitfiles-iresourcebuilder-mongodbserverresource-string) instead.

## ATS metadata

### Ignored by ATS

- Excluded from automatic Polyglot export.

## WithInitFiles(IResourceBuilder<MongoDBServerResource>, string)

- Name: `WithInitFiles(IResourceBuilder<MongoDBServerResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<MongoDBServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L257-L264)

Copies init files into a MongoDB container resource.

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<MongoDBServerResource> WithInitFiles(
        this IResourceBuilder<MongoDBServerResource> builder,
        string source)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<MongoDBServerResource>`)
  The resource builder.
- `source` (`string`)
  The source file or directory on the host to copy into the container.

## Returns

`IResourceBuilder<MongoDBServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithMongoExpress(IResourceBuilder<T>, Action<IResourceBuilder<MongoExpressContainerResource>>, string?)

- Name: `WithMongoExpress(IResourceBuilder<T>, Action<IResourceBuilder<MongoExpressContainerResource>>, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<T>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L160-L175)

Adds a MongoExpress administration and development platform for MongoDB to the application model.

```csharp
public static class MongoDBBuilderExtensions
{
    public static IResourceBuilder<T> WithMongoExpress<T>(
        this IResourceBuilder<T> builder,
        Action<IResourceBuilder<MongoExpressContainerResource>>? configureContainer = null,
        string? containerName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<T>`)
  The MongoDB server resource builder.
- `configureContainer` (`Action<IResourceBuilder<MongoExpressContainerResource>>`) `optional`
  Configuration callback for Mongo Express container resource.
- `containerName` (`string?`) `optional`
  The name of the container (Optional).

## Returns

`IResourceBuilder<T>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

This version of the package defaults to the tag of the container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
