Watch Aspire live streamsDocsTry Aspire
Watch Aspire live streamsDocsTry

MongoDBBuilderExtensions Methods

ClassMethods15 members
Provides extension methods for adding MongoDB resources to an Hosting.IDistributedApplicationBuilder.
AddDatabase(IResourceBuilder<MongoDBServerResource>, string, string?)Section titled AddDatabase(IResourceBuilder<MongoDBServerResource>, string, string?)extensionIResourceBuilder<MongoDBDatabaseResource>
Adds a MongoDB database to the application model.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(
this IResourceBuilder<MongoDBServerResource> builder,
string name,
string? databaseName = null)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The MongoDB server resource builder.
namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.
databaseNamestring?optionalThe name of the database. If not provided, this defaults to the same value as name.
IResourceBuilder<MongoDBDatabaseResource>A reference to the ApplicationModel.IResourceBuilder`1.
AddMongoDB(IDistributedApplicationBuilder, string, int?)Section titled AddMongoDB(IDistributedApplicationBuilder, string, int?)extensionIResourceBuilder<MongoDBServerResource>
Adds a MongoDB resource to the application model. A container is used for local development.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> AddMongoDB(
this IDistributedApplicationBuilder builder,
string name,
int? port)
{
// ...
}
}
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.
namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.
portint?The host port for MongoDB.
IResourceBuilder<MongoDBServerResource>A reference to the ApplicationModel.IResourceBuilder`1.

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

This overload is not available in polyglot AppHosts. Use MongoDBBuilderExtensions.AddMongoDB instead.

AddMongoDB(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)Section titled AddMongoDB(IDistributedApplicationBuilder, string, int?, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<MongoDBServerResource>
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)
{
// ...
}
}
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.
namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.
portint?optionalThe host port for MongoDB.
userNameIResourceBuilder<ParameterResource>optionalA parameter that contains the MongoDb server user name, or null to use a default value.
passwordIResourceBuilder<ParameterResource>optionalA parameter that contains the MongoDb server password, or null to use a generated password.
IResourceBuilder<MongoDBServerResource>A reference to the ApplicationModel.IResourceBuilder`1.
WithBindIpAll(IResourceBuilder<MongoDBServerResource>)Section titled WithBindIpAll(IResourceBuilder<MongoDBServerResource>)extensionIResourceBuilder<MongoDBServerResource>
Configures the MongoDB server to bind to and listen on all network interfaces.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithBindIpAll(
this IResourceBuilder<MongoDBServerResource> builder)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The MongoDB server resource builder.
IResourceBuilder<MongoDBServerResource>A reference to the ApplicationModel.IResourceBuilder`1.
See https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-net.bindIpAll
WithDataBindMount(IResourceBuilder<MongoDBServerResource>, string, bool)Section titled WithDataBindMount(IResourceBuilder<MongoDBServerResource>, string, bool)extensionIResourceBuilder<MongoDBServerResource>
Adds a bind mount for the data folder to a MongoDB container resource.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithDataBindMount(
this IResourceBuilder<MongoDBServerResource> builder,
string source,
bool isReadOnly = false)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The resource builder.
sourcestringThe source directory on the host to mount into the container.
isReadOnlybooloptionalA flag that indicates if this is a read-only mount.
IResourceBuilder<MongoDBServerResource>The ApplicationModel.IResourceBuilder`1.
WithDataVolume(IResourceBuilder<MongoDBServerResource>, string?, bool)Section titled WithDataVolume(IResourceBuilder<MongoDBServerResource>, string?, bool)extensionIResourceBuilder<MongoDBServerResource>
Adds a named volume for the data folder to a MongoDB container resource.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithDataVolume(
this IResourceBuilder<MongoDBServerResource> builder,
string? name = null,
bool isReadOnly = false)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The resource builder.
namestring?optionalThe name of the volume. Defaults to an auto-generated name based on the application and resource names.
isReadOnlybooloptionalA flag that indicates if this is a read-only volume.
IResourceBuilder<MongoDBServerResource>The ApplicationModel.IResourceBuilder`1.
WithHostPort(IResourceBuilder<MongoExpressContainerResource>, int?)Section titled WithHostPort(IResourceBuilder<MongoExpressContainerResource>, int?)extensionIResourceBuilder<MongoExpressContainerResource>
Configures the host port that the Mongo Express resource is exposed on instead of using randomly assigned port.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoExpressContainerResource> WithHostPort(
this IResourceBuilder<MongoExpressContainerResource> builder,
int? port)
{
// ...
}
}
builderIResourceBuilder<MongoExpressContainerResource>The resource builder for Mongo Express.
portint?The port to bind on the host. If null is used random port will be assigned.
IResourceBuilder<MongoExpressContainerResource>A reference to the ApplicationModel.IResourceBuilder`1.
WithInitBindMount(IResourceBuilder<MongoDBServerResource>, string, bool)Section titled WithInitBindMount(IResourceBuilder<MongoDBServerResource>, string, bool)extensionIResourceBuilder<MongoDBServerResource>
Adds a bind mount for the init folder to a MongoDB container resource.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithInitBindMount(
this IResourceBuilder<MongoDBServerResource> builder,
string source,
bool isReadOnly = true)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The resource builder.
sourcestringThe source directory on the host to mount into the container.
isReadOnlybooloptionalA flag that indicates if this is a read-only mount.
IResourceBuilder<MongoDBServerResource>The ApplicationModel.IResourceBuilder`1.
This method is not available in polyglot AppHosts. Use MongoDBBuilderExtensions.WithInitFiles instead.
WithInitFiles(IResourceBuilder<MongoDBServerResource>, string)Section titled WithInitFiles(IResourceBuilder<MongoDBServerResource>, string)extensionIResourceBuilder<MongoDBServerResource>
Copies init files into a MongoDB container resource.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithInitFiles(
this IResourceBuilder<MongoDBServerResource> builder,
string source)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The resource builder.
sourcestringThe source file or directory on the host to copy into the container.
IResourceBuilder<MongoDBServerResource>The ApplicationModel.IResourceBuilder`1.
WithKeyFile(IResourceBuilder<MongoDBServerResource>, IExpressionValue, string)Section titled WithKeyFile(IResourceBuilder<MongoDBServerResource>, IExpressionValue, string)extensionIResourceBuilder<MongoDBServerResource>
Sets up a keyfile for internal authentication between members of a MongoDB replica set, with the specified keyValue as the content of the file.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithKeyFile(
this IResourceBuilder<MongoDBServerResource> builder,
IExpressionValue keyValue,
string keyFilePath = "/etc/rs.key")
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The MongoDB server resource builder.
keyValueIExpressionValueThe content of the keyfile. This is a shared secret: every member of the replica set has to be given the same value, and anything holding it can authenticate as a member of the cluster.
keyFilePathstringoptionalThe absolute path the keyfile is mounted at inside the container, which has to include a file name. Defaults to /etc/rs.key.
IResourceBuilder<MongoDBServerResource>A reference to the ApplicationModel.IResourceBuilder`1.
The keyfile is a shared secret. Every member of the replica set (or sharded cluster) should have the same keyfile, and possession of that secret is what authenticates a connection as "a legitimate member of this cluster." See https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set-with-keyfile-access-control/
WithMongoExpress(IResourceBuilder<T>, Action<IResourceBuilder<MongoExpressContainerResource>>, string?)Section titled WithMongoExpress(IResourceBuilder<T>, Action<IResourceBuilder<MongoExpressContainerResource>>, string?)extensionIResourceBuilder<T>
Adds a MongoExpress administration and development platform for MongoDB to the application model.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<T> WithMongoExpress<T>(
this IResourceBuilder<T> builder,
Action<IResourceBuilder<MongoExpressContainerResource>>? configureContainer = null,
string? containerName = null)
{
// ...
}
}
builderIResourceBuilder<T>The MongoDB server resource builder.
configureContainerAction<IResourceBuilder<MongoExpressContainerResource>>optionalConfiguration callback for Mongo Express container resource.
containerNamestring?optionalThe name of the container (Optional).
IResourceBuilder<T>A reference to the ApplicationModel.IResourceBuilder`1.
This version of the package defaults to the tag of the container image.
WithRepl(IResourceBuilder<MongoDBServerResource>)Section titled WithRepl(IResourceBuilder<MongoDBServerResource>)extensionIResourceBuilder<MongoDBServerResource>
Adds a REPL command that opens an authenticated MongoDB shell in the dashboard terminal dock.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithRepl(
this IResourceBuilder<MongoDBServerResource> builder)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The MongoDB server resource builder.
IResourceBuilder<MongoDBServerResource>The resource builder for chaining.
ArgumentNullExceptionbuilder is null.
This command is opt-in and available only in run mode. Dashboard users who can execute resource commands can run commands with the resource's configured credentials. Enable it only for trusted dashboard users, especially when sharing the dashboard through a tunnel or remote development environment.
builder.AddMongoDB("mongo").WithRepl();
WithReplicaSet(IResourceBuilder<MongoDBServerResource>, string?)Section titled WithReplicaSet(IResourceBuilder<MongoDBServerResource>, string?)extensionIResourceBuilder<MongoDBServerResource>
Configures and initializes the MongoDB server as a single-member replica set for local development.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithReplicaSet(
this IResourceBuilder<MongoDBServerResource> builder,
string? name = null)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The MongoDB server resource builder.
namestring?optionalThe replica set name. Defaults to the resource name, or the name previously configured by this method.
IResourceBuilder<MongoDBServerResource>A reference to the ApplicationModel.IResourceBuilder`1.
ArgumentNullExceptionThe builder is null.
ArgumentExceptionThe name is empty or whitespace.
InvalidOperationExceptionThe server is configured for a different set or for advanced membership.
NotSupportedExceptionThe application is running in publish mode.

Enables transactions and change streams without changing database resources or references. The server becomes healthy only after initialization and primary election. Connections use the server's normal endpoint directly; a developer certificate, topology discovery, and fixed host ports are not required.

A key file is generated for authentication unless MongoDBBuilderExtensions.WithKeyFile was called first. Existing single-member data is reused without reconfiguration. Keep the set name and credentials unchanged when reusing a data volume. Publishing and deploying this configuration are not supported.

Unlike the previous low-level behavior of this experimental method, this method initializes the set. For multiple members, use MongoDBReplicaSetBuilderExtensions.AddMongoDBReplicaSet and MongoDBReplicaSetBuilderExtensions.WithMember instead, without calling this method on the members.

var mongo = builder.AddMongoDB("mongo").WithReplicaSet();
var database = mongo.AddDatabase("orders");
builder.AddProject<Projects.Api>("api").WithReference(database).WaitFor(database);
WithTlsAllowInvalidCertificates(IResourceBuilder<MongoDBServerResource>)Section titled WithTlsAllowInvalidCertificates(IResourceBuilder<MongoDBServerResource>)extensionIResourceBuilder<MongoDBServerResource>
Configures the MongoDB server to accept TLS connections whose peer certificate cannot be validated, by passing --tlsAllowInvalidCertificates to mongod.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithTlsAllowInvalidCertificates(
this IResourceBuilder<MongoDBServerResource> builder)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The MongoDB server resource builder.
IResourceBuilder<MongoDBServerResource>A reference to the ApplicationModel.IResourceBuilder`1.

This weakens certificate authentication and should only be used when a peer legitimately cannot present a certificate this server is able to validate. It is what MongoDBReplicaSetBuilderExtensions.WithMember uses today, because replica set members authenticate to each other with the same certificate they serve to clients and that certificate does not carry a clientAuth extended key usage.

See https://www.mongodb.com/docs/manual/reference/program/mongod/#std-option-mongod.--tlsAllowInvalidCertificates

var mongo = builder.AddMongoDB("mongo")
.WithTlsAllowInvalidCertificates();
WithTlsMode(IResourceBuilder<MongoDBServerResource>, MongoDBTlsMode)Section titled WithTlsMode(IResourceBuilder<MongoDBServerResource>, MongoDBTlsMode)extensionIResourceBuilder<MongoDBServerResource>
Configures the --tlsMode the MongoDB server is started with when TLS is active.
public static class MongoDBBuilderExtensions
{
public static IResourceBuilder<MongoDBServerResource> WithTlsMode(
this IResourceBuilder<MongoDBServerResource> builder,
MongoDBTlsMode mode = MongoDBTlsMode.RequireTls)
{
// ...
}
}
builderIResourceBuilder<MongoDBServerResource>The MongoDB server resource builder.
modeMongoDBTlsModeoptionalThe TLS mode to run the MongoDB server in.
IResourceBuilder<MongoDBServerResource>A reference to the ApplicationModel.IResourceBuilder`1.

TLS itself is not turned on by this method. A MongoDB server resource serves TLS whenever an HTTPS/TLS certificate is available for it — by default the ASP.NET Core developer certificate — and serves plain TCP otherwise. Use ResourceBuilderExtensions.WithHttpsDeveloperCertificate or ResourceBuilderExtensions.WithHttpsCertificate to opt in explicitly, and ResourceBuilderExtensions.WithoutHttpsCertificate to opt out. This method only controls how strict the server is about TLS on incoming connections once TLS is active; the default is MongoDBTlsMode.RequireTls.

See https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.mode

Let clients connect either with or without TLS:
var mongo = builder.AddMongoDB("mongo")
.WithTlsMode(MongoDBTlsMode.PreferTls);