package io.quarkus.mongodb.health;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.spi.Bean;

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
import org.eclipse.microprofile.health.Readiness;

import com.mongodb.client.MongoClient;

import io.quarkus.arc.Arc;

@Readiness
@ApplicationScoped
public class MongoHealthCheck implements HealthCheck {
    private static final String DEFAULT_CLIENT = "__default__";
    private Map<String, MongoClient> clients = new HashMap<>();

    @PostConstruct
    protected void init() {
        Set<Bean<?>> beans = Arc.container().beanManager().getBeans(MongoClient.class);
        for (Bean<?> bean : beans) {
            if (bean.getName() == null) {
                // this is the default mongo client: retrieve it by type
                MongoClient defaultClient = Arc.container().instance(MongoClient.class).get();
                clients.put(DEFAULT_CLIENT, defaultClient);
            } else {
                MongoClient client = (MongoClient) Arc.container().instance(bean.getName()).get();
                clients.put(bean.getName(), client);
            }
        }
    }

    @Override
    public HealthCheckResponse call() {
        HealthCheckResponseBuilder builder = HealthCheckResponse.named("MongoDB connection health check").up();
        for (Map.Entry<String, MongoClient> client : clients.entrySet()) {
            boolean isDefault = DEFAULT_CLIENT.equals(client.getKey());
            MongoClient mongoClient = client.getValue();
            try {
                StringBuilder databases = new StringBuilder();
                for (String db : mongoClient.listDatabaseNames()) {
                    if (databases.length() != 0) {
                        databases.append(", ");
                    }
                    databases.append(db);
                }
                String mongoClientName = isDefault ? "default" : client.getKey();
                builder.up().withData(mongoClientName, databases.toString());
            } catch (Exception e) {
                return builder.down().withData("reason", e.getMessage()).build();
            }
        }
        return builder.build();
    }
}

@@ -232,16 +232,42 @@
 .Bean;%0A%0A
+import org.bson.Document;%0A
 import o
@@ -1824,87 +1824,27 @@
     
-StringBuilder databases = new StringBuilder();%0A                for (String db :
+Document document =
  mon
@@ -1856,11 +1856,10 @@
 ent.
-lis
+ge
 tDat
@@ -1867,199 +1867,54 @@
 base
-Names()) %7B%0A                    if (databases.length() != 0) %7B%0A                        databases.append(%22, %22);%0A                    %7D%0A                    databases.append(db);%0A                %7D
+(%22admin%22).runCommand(new Document(%22ping%22, 1));
 %0A   
@@ -2052,25 +2052,22 @@
 e, d
-atabases.toString
+ocument.toJson
 ());

Use the ping command inside the health check