MongoClient
suspend fun MongoClient(hostname: String = "localhost", port: Int = 27017, coroutineContext: CoroutineContext, bsonFactory: BsonFactory = BsonFactory(), objectIdGenerator: ObjectIdGenerator = ObjectIdGenerator.Default(), propertyNameStrategy: PropertyNameStrategy = PropertyNameStrategy.Default): MongoClient(source)
Connects to the database at the specified hostname and port.
By default, connects to "mongo://localhost:27017".
Example
val job = Job()
val client = MongoClient(coroutineContext = job)
val collection = client.database("mydb").collection<User>("mycollection")
println(collection.count())
job.cancel("Shutting down the client")Content copied to clipboard
Parameters
coroutineContext
The coroutine context used to maintain the connection, including background tasks. Specify a custom Job to control the lifecycle of the client (call Job.cancel to close the client).
bsonFactory
The BsonFactory instance used to serialize and deserialize BSON values. Pass a custom instance to configure polymorphic serialization and other matters.
objectIdGenerator
The algorithm used to generate new ObjectId instances.
propertyNameStrategy
The algorithm used to convert from the DSL path syntax accesses to MongoDB field paths.