Skip to main content

Mongo

A thin wrapper around MongoClient and MongoDatabase from the MongoDB Kotlin driver. The database name is parsed from the connection string — an explicit database name in the URI is required.

class Mongo(val connectionString: String) {
val databaseName: String
val database: MongoDatabase
fun <Result> runInTransaction(block: (ClientSession) -> Result): Result
fun close()
}

Construction

val mongo = Mongo("mongodb://localhost:27017/myapp")
mongo.databaseName // "myapp"
mongo.database // com.mongodb.kotlin.client.MongoDatabase

The first access to database pings the server and logs success. Without a database segment in the URI the constructor throws IllegalArgumentException.

Transactions

runInTransaction opens a session with WriteConcern.MAJORITY, executes the block and commits. On exception the transaction is aborted and the exception is rethrown.

val updated = mongo.runInTransaction { session ->
productCollection.txUpdate(session, product)
auditCollection.txInsert(session, AuditEntry(...))
product
}

Client session interop

invirt-mongodb exposes a JavaClientSession typealias for com.mongodb.client.ClientSession and a JavaClientSession.kotlin() extension to convert it into the Kotlin driver's session type. This is used internally by the Mongock migrations helpers.