Docs Menu
Docs Home
/ / /
Kotlin Sync Driver
/

Create a MongoClient

On this page

  • Overview
  • Connection URI
  • Atlas Connection Example
  • API Documentation

To connect to a MongoDB deployment, you need two things:

  • A connection URI, also known as a connection string, which tells the Kotlin Sync driver which MongoDB deployment to connect to.

  • A MongoClient object, which creates the connection to and performs operations on the MongoDB deployment.

You can also use MongoClientSettings to customize the way the Kotlin Sync driver behaves while connected to MongoDB.

This guide shows you how to create a connection string and use a MongoClient object to connect to MongoDB.

A standard connection string includes the following components:

Component
Description
mongodb://
Required. A prefix that identifies this as a string in the standard connection format.
username:password
Optional. Authentication credentials. If you include these, the client authenticates the user against the database specified in authSource.
host[:port]
Required. The host and optional port number where MongoDB is running. If you don't include the port number, the driver uses the default port, 27017.
/defaultauthdb
Optional. The authentication database to use if the connection string includes username:password@ authentication credentials but not the authSource option. If you don't include this component, the client authenticates the user against the admin database.
?<options>
Optional. A query string that specifies connection-specific options as <name>=<value> pairs. See Specify Connection Options for a full description of these options.

For more information about creating a connection string, see Connection Strings in the MongoDB Server documentation.

To connect to a MongoDB deployment on Atlas, you must first create a client.

You can pass a connection URI as a string to the MongoClient.create() method to connect to a MongoDB instance:

// Replace the placeholder with your Atlas connection string
val uri = "<connection string>"
// Create a new client and connect to the server
val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("sample_mflix")

You can also create a client with your desired configurations by passing a MongoClientSettings object to the MongoClient.create() method.

To instantiate a MongoClientSettings object, use the builder method to specify your connection string, using the applyConnectionString() method, and any other client options. Once you have your desired configuration, call the build() method.

You can set the Stable API version client option to avoid breaking changes when you upgrade to a new server version. To learn more about the Stable API feature, see the Stable API page.

The following code shows how you can specify the connection string and the Stable API client option when connecting to a MongoDB deployment on Atlas and verify that the connection is successful:

// start-connect-to-atlas-w-uri
// Replace the placeholder with your Atlas connection string
val uri = "<connection string>"
val mongoClient1 = MongoClient.create(uri)
// end-connect-to-atlas-w-uri
// Construct a ServerApi instance using the ServerApi.builder() method
val serverApi = ServerApi.builder()
.version(ServerApiVersion.V1)
.build()
val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString(uri))
.serverApi(serverApi)
.build()
// Create a new client and connect to the server
val mongoClient = MongoClient.create(settings)
val database = mongoClient.getDatabase("sample_mflix")
try {
// Send a ping to confirm a successful connection
val command = Document("ping", BsonInt64(1))
val commandResult = database.runCommand(command)
println("Pinged your deployment. You successfully connected to MongoDB!")
} catch (me: MongoException) {
System.err.println(me)
}

For more information about creating a MongoClient object with the Kotlin Sync driver, see the following API documentation:

Back

Connect to MongoDB