MOngoDB Spring Data Driver

# Introduction

This sections covers the Mongock implementation for Spring Data MongoDB 2.x and 3.x



# MongoDB driver options and compatibility

Mongock driver Driver library Version compatibility
mongodb-springdata-v3-driver org.springframework.data:spring-data-mongodb 3.X.X
mongodb-springdata-v2-driver org.springframework.data:spring-data-mongodb 2.X.X


# MongoDB common configuration

All the MongoDB drivers share the same configuration.

When setting configuration via properties file, it must be prefixed by mongock.mongo-db

# Properties

Property Description Type Default value
writeConcern Exactly the same MongoDB parameter write concern. For more information, visit the official MongoDB documentation for write concern. Object {w:majority,
wTimeoutMs: null,
j:true}
readConcern Exactly the same MongoDB parameter read concern. For more information, visit the official MongoDB documentation for read concern. String majority
readPreference Exactly the same MongoDB parameter read preference. For more information, visit the official MongoDB documentation for read preference. String primary


# Get started

Following the get started section, this covers steps 3 and 5 and 6.

# - Add maven dependency for the driver (step 2)

<dependency>
<groupId>io.mongock</groupId>
<artifactId>mongodb-springdata-v3-driver</artifactId>
<!--<artifactId>mongodb-springdata-v2-driver</artifactId> for MongoDB spring data v2-->
</dependency>

# - Build the driver (step 5)

This step is only required for builder approach. Mongock handles it for autoconfiguration.

These classes provide the same two static initializers:

// For mongodb-springdata-v3-driver
SpringDataMongoV3Driver driver = SpringDataMongoV3Driver.withDefaultLock(mongoTemplate);
// For mongodb-springdata-v2-driver
//SpringDataMongoV2Driver driver = SpringDataMongoV2Driver.withDefaultLock(mongoTemplate);
driver.setWriteConcern(WriteConcern.MAJORITY.withJournal(true).withWTimeout(1000, TimeUnit.MILLISECONDS));
driver.setReadConcern(ReadConcern.MAJORITY);
driver.setReadPreference(ReadPreference.primary());
driver.enableTransaction();

# - Driver extra configuration (step 6)

# Transactions

In order to use MongoDB transactions, we need to enable transactions in Mongock(this won't be required in next versions as transactions will be enabled by default).

With builder

driver.enableTransaction();

Properties

mongock.transactional=true

Keep in mind that your MongoDB database must allow multi-document ACID transactions


# Example autoconfiguration with Springboot

mongock:
mongo-db:
write-concern:
w: majority
wTimeoutMs: 1000
journal: true
read-concern: majority
read-preference: primary
@EnableMongock
@SpringBootApplication
public class QuickStartApp {
/**
* Be warned MongoTemplate needs to be injected
*/
public static void main(String[] args) {
SpringApplicationBuilder().sources(QuickStartApp.class)().run(args);
}
}


# Examples

Please visit our example github repository for more information