Skip to content

Commit bd22f1e

Browse files
authored
Add compatibility with MongoDB 6 (#5771)
MongoDB 6 removed `mongo` shell and `mongosh` is recommended. See [here](https://www.mongodb.com/docs/manual/release-notes/6.0-compatibility/#legacy-mongo-shell-removed) Closes #5768
1 parent a18cab6 commit bd22f1e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.SneakyThrows;
66
import lombok.extern.slf4j.Slf4j;
77
import org.testcontainers.containers.wait.strategy.Wait;
8+
import org.testcontainers.utility.ComparableVersion;
89
import org.testcontainers.utility.DockerImageName;
910

1011
import java.io.IOException;
@@ -29,6 +30,8 @@ public class MongoDBContainer extends GenericContainer<MongoDBContainer> {
2930

3031
private static final String MONGODB_DATABASE_NAME_DEFAULT = "test";
3132

33+
private final boolean isAtLeastVersion6;
34+
3235
/**
3336
* @deprecated use {@link MongoDBContainer(DockerImageName)} instead
3437
*/
@@ -45,6 +48,8 @@ public MongoDBContainer(final DockerImageName dockerImageName) {
4548
super(dockerImageName);
4649
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
4750

51+
this.isAtLeastVersion6 = new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("6.0");
52+
4853
withExposedPorts(MONGODB_INTERNAL_PORT);
4954
withCommand("--replSet", "docker-rs");
5055
waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 1));
@@ -87,7 +92,8 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
8792
}
8893

8994
private String[] buildMongoEvalCommand(final String command) {
90-
return new String[] { "mongo", "--eval", command };
95+
String cmd = this.isAtLeastVersion6 ? "mongosh" : "mongo";
96+
return new String[] { cmd, "--eval", command };
9197
}
9298

9399
private void checkMongoNodeExitCode(final Container.ExecResult execResult) {

modules/mongodb/src/test/java/org/testcontainers/containers/MongoDBContainerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,11 @@ public void shouldTestDatabaseName() {
9898
assertThat(mongoDBContainer.getReplicaSetUrl(databaseName)).endsWith(databaseName);
9999
}
100100
}
101+
102+
@Test
103+
public void supportsMongoDB_6() {
104+
try (final MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:6.0.1")) {
105+
mongoDBContainer.start();
106+
}
107+
}
101108
}

0 commit comments

Comments
 (0)