MongoDB.local SF, Jan 15: See the speaker lineup & ship your AI vision faster. Use WEB50 to save 50%
Find out more >
Docs Menu
Docs Home
/ /
/ / /

Create a MongoClient

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

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

  • A MongoClient object, which creates the connection to the MongoDB deployment and lets you perform operations on it.

You can also use either of these components to customize the way Kotlin 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.

The connection URI provides a set of instructions that the driver uses to connect to a MongoDB deployment. It instructs the driver on how it should connect to MongoDB and how it should behave while connected. The following figure explains each part of a sample connection URI:

Connection String parts figure

This figure uses the Standard Connection String Format, mongodb for the protocol. You can also use the DNS Seed List Connection Format, mongodb+srv, if you want more flexibility of deployment and the ability to change the servers in rotation without reconfiguring clients.

Note

If your deployment is on MongoDB Atlas, see the Atlas driver connection guide and select Kotlin from the language dropdown to retrieve your connection string.

The next part of the connection URI contains your credentials if you are using a password-based authentication mechanism. Replace the value of user with your database username and pass with your database user's password. If your authentication mechanism does not require credentials, omit this part of the connection URI.

The next part of the connection URI specifies the hostname or IP address, followed by the port of your MongoDB instance. In the example, sample.host represents the hostname and 27017 is the port number. Replace these values to refer to your MongoDB instance.

The last part of the connection URI contains connection options as parameters. In the example, we set two connection options: maxPoolSize=20 and w=majority. For more information on connection options, see the Connection Options guide.

You can connect to and communicate with MongoDB by using the MongoClient class.

Use the MongoClient.create() method to construct a MongoClient.

The following code shows how to create a MongoClient that connects to a local MongoDB deployment:

import com.mongodb.kotlin.client.coroutine.MongoClient
val uri = "mongodb://localhost:27017/"
val client = MongoClient.create(uri)

Important

Reuse Your Client

As each MongoClient represents a thread-safe pool of connections to the database, most applications only require a single instance of a MongoClient, even across multiple threads. All resource usage limits, such as max connections, apply to individual MongoClient instances.

To learn about the different settings you can use to control the behavior of your MongoClient, see the MongoClient Settings guide.

Tip

Always call MongoClient.close() to clean up resources when an instance is no longer needed.

For more information about the methods and classes mentioned in this section, see the following API documentation:

Back

Connect

On this page