SyncMongoClient¶
interface SyncMongoClient : MongoClient
Entry-point to the KtMongo Coroutines driver.
The Coroutine client provides a coroutine-aware API which internally uses the official Kotlin driver.
Organizing data¶
Accessing MongoDB data happens in three steps:
-
SyncMongoClient: represents the connection to the MongoDB application, handles the lifecycle and the configuration. -
SyncMongoDatabase(accessed withSyncMongoClient.database): each database groups data together. This allows deploying multiple applications (or the same application multiple times) without name collisions. -
SyncMongoCollection(accessed withSyncMongoDatabase.collection): each collection stores data together. Documents in a collection may have a different structure.
Example¶
@Serializable
class User(
val _id: ObjectId,
val name: String,
val age: Int,
)
fun main() = runBlocking {
val client = SyncMongoClient("mongodb://localhost:27017")
val database = client.database("my-app")
val users = database.collection<User>("users")
println("The database contains ${users.count()} users.")
}
See also¶
- asKtMongoLegacy: Convert an existing instance from the official Kotlin driver.
Constructors¶
SyncMongoClient¶
fun SyncMongoClient(connectionString: String = "mongodb://localhost:27017"): SyncMongoClient
Instantiates a KtMongo SyncMongoClient using an existing client from the official Kotlin driver.
This method allows taking advantage of the full configuration power of the official client.
Example
Functions¶
asOfficial¶
abstract fun asOfficial(): MongoClient
Obtains the underlying MongoDB client from the official Kotlin driver.
close¶
abstract fun close()
database¶
abstract override fun database(name: String): SyncMongoDatabase