MongoClient
Entry-point to the KtMongo Multiplatform driver.
Organizing data
Accessing MongoDB data happens in three steps:
MongoClient: represents the connection to the MongoDB application, handles the lifecycle and the configuration.
MongoDatabase (accessed with MongoClient.database): each database groups data together. This allows deploying multiple applications (or the same application multiple times) without name collisions.
MongoCollection (accessed with MongoDatabase.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 = MongoClient(
hostname = "localhost",
port = 27017,
coroutineContext = currentCoroutineContext(),
)
val database = client.database("my-app")
val users = database.collection<User>("users")
println("The database contains ${users.count()} users.")
}Content copied to clipboard