Installation
Install the MongoDB hosting integration package in your AppHost project:Usage
Add MongoDB Server and Database
In your AppHost project (AppHost.cs), add a MongoDB server and database:
Consume in Service
In your service project, reference the database using the client integration:Program.cs:
API Reference
AddMongoDB
Adds a MongoDB server resource to the application model. A container is used for local development.Parameters
name- The name of the resource. This name will be used as the connection string name when referenced in a dependency.port- The host port to bind the underlying container to. If null, a random port will be assigned.userName- The parameter used to provide the user name for the MongoDB resource. Ifnulla default value will be used.password- The parameter used to provide the administrator password for the MongoDB resource. Ifnulla random password will be generated.
Returns
A reference to theIResourceBuilder<MongoDBServerResource>.
AddDatabase
Adds a MongoDB database to the application model.Parameters
name- The name of the resource. This name will be used as the connection string name when referenced in a dependency.databaseName- The MongoDB database name. If not provided, defaults to the same value asname.
Configuration Examples
Custom Port
Custom Credentials
Data Persistence with Volumes
Multiple Databases
Mongo Express Management Tool
Named Volumes
Bind Mounts
Connection Properties
When you reference a MongoDB resource usingWithReference, the following connection properties are made available to the consuming project.
MongoDB Server
| Property Name | Description |
|---|---|
Host | The hostname or IP address of the MongoDB server |
Port | The port number the MongoDB server is listening on |
Username | The username for authentication |
Password | The password for authentication (available when a password parameter is configured) |
AuthenticationDatabase | The authentication database (available when a password parameter is configured) |
AuthenticationMechanism | The authentication mechanism (available when a password parameter is configured) |
Uri | The connection URI, with the format mongodb://{Username}:{Password}@{Host}:{Port}/?authSource={AuthenticationDatabase}&authMechanism={AuthenticationMechanism} |
MongoDB Database
The MongoDB database resource combines the server properties above and adds:| Property Name | Description |
|---|---|
DatabaseName | The MongoDB database name |
Environment Variables
Aspire exposes each property as an environment variable named[RESOURCE]_[PROPERTY]. For instance:
- The
Uriproperty of a resource calledmydbbecomesMYDB_URI - The
DatabaseNameproperty becomesMYDB_DATABASENAME
Health Checks
The MongoDB hosting integration includes built-in health checks. When a resource is referenced as a dependency usingWaitFor, the dependent resource will wait until the MongoDB resource is able to service requests.