Package reference page maintained from the source documentation in src/HealthChecks.MongoDb.

MongoDB Health Check

This health check verifies the ability to communicate with MongoDB. It uses the provided MongoClient to list database names or ping configured database.

Defaults

By default, the MongoClient instance is resolved from service provider.

void Configure(IHealthChecksBuilder builder)
{
    builder.Services
        .AddSingleton(sp => new MongoClient("mongodb://localhost:27017"))
        .AddHealthChecks()
        .AddMongoDb();
}

Customization

You can additionally add the following parameters:

void Configure(IHealthChecksBuilder builder)
{
    builder.Services
        .AddSingleton(sp => new MongoClient("mongodb://localhost:27017"))
        .AddHealthChecks()
        .AddMongoDb(databaseNameFactory: sp => "theName");
}

Breaking changes

MongoDbHealthCheck was letting the users specify how MongoClient should be created (from raw connection string or from MongoUrl or from MongoClientSettings), at a cost of maintaining an internal, static client instances cache. Now the type does not create client instances nor maintain an internal cache and it's the caller responsibility to provide the instance of MongoClient (please see #2048 for more details). Since MongoDB recommends treating clients as singletons and client instances can be expensive to create, it's recommended to register a singleton factory method for MongoClient. So the client is created only when needed and once per whole application lifetime.