As I mentioned in the other answer, the problem is that Cosmos DB isn't fully compatible with MongoDB. It does support the shell command db.hello(),
but it doesn't support the database command db.runCommand({hello: 1})
.
Currently there's no solution to this problem as it's something Microsoft has to fix themselves. A workaround for Spring Boot is to disable the existing healthcheck by setting the management.health.mongo.enabled
property and by defining your own health indicator. Calling a shell command from the Java driver isn't possible as far as I know, so I would use another database command in stead.
I suggest using the buildInfo
database command and to write an indicator like this:
@Component
public class MongoHealthIndicator extends AbstractHealthIndicator {
private final MongoTemplate mongoTemplate;
public MongoHealthIndicator(MongoTemplate mongoTemplate) {
super("MongoDB health check failed");
Assert.notNull(mongoTemplate, "'mongoTemplate' must not be null");
this.mongoTemplate = mongoTemplate;
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Document result = this.mongoTemplate.executeCommand("{ buildInfo: 1 }");
builder.up();
}
}
Alternatively, you could downgrade to any Spring Boot version before 3.2.7, but I wouldn't recommend that. For a full overview and my implementation of the reactive health indicator you could check my dedicated blogpost about it.