docs/modules/databases/mongodb.md
The MongoDB module provides two Testcontainers for MongoDB unit testing:
The following example shows how to create a MongoDBContainer:
<!--codeinclude-->Creating a MongoDB container inside_block:creatingMongoDBContainer
<!--/codeinclude-->And how to start it:
<!--codeinclude-->Starting a MongoDB container inside_block:startingMongoDBContainer
<!--/codeinclude-->!!! note To construct a multi-node MongoDB cluster, consider the mongodb-replica-set project
Implement a reusable, cross-platform, simple to install solution that doesn't depend on fixed ports to test MongoDB transactions.
MongoDB starting from version 4 supports multi-document transactions only for a replica set. For instance, to initialize a single node replica set on fixed ports via Docker, one has to do the following:
As we can see, there is a lot of operations to execute and we even haven't touched a non-fixed port approach. That's where the MongoDBContainer might come in handy.
The following example shows how to create a MongoDBAtlasLocalContainer:
<!--codeinclude-->Creating a MongoDB Atlas Local Container inside_block:creatingAtlasLocalContainer
<!--/codeinclude-->And how to start it:
<!--codeinclude-->Start the Container inside_block:startingAtlasLocalContainer
<!--/codeinclude-->The connection string provided by the MongoDBAtlasLocalContainer's getConnectionString() method includes the dynamically allocated port:
<!--codeinclude-->Get the Connection String inside_block:getConnectionStringAtlasLocalContainer
<!--/codeinclude-->e.g. mongodb://localhost:12345/?directConnection=true
MongoDB Atlas Local combines the MongoDB database engine with MongoT, a sidecar process for advanced searching capabilities built by MongoDB and powered by Apache Lucene.
The container (mongodb/mongodb-atlas-local) documentation can be found here.
General information about Atlas Search can be found here.
Add the following dependency to your pom.xml/build.gradle file:
=== "Gradle"
groovy testImplementation "org.testcontainers:testcontainers-mongodb:{{latest_version}}"
=== "Maven"
xml <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers-mongodb</artifactId> <version>{{latest_version}}</version> <scope>test</scope> </dependency>
!!! hint Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency
Copyright (c) 2019 Konstantin Silaev [email protected]