Making a connection

In order to create a connection to a MongoDB database, an instance of MongoClient[F] class needs to be instantiated. The MongoClient[F] instance represents a pool of connections for a given MongoDB server deployment and typically only one instance of this class is required per application (even with multiple operations executed concurrently).

There are multiple ways of creating a client:

import cats.effect.IO
import mongo4cats.client._

// From a connection string
val client = MongoClient.fromConnectionString[IO]("mongodb://localhost:27017")

// By providing ServerAddress object
val client = MongoClient.fromServerAddress[IO](ServerAddress("localhost", 27017))

// By providing custom MongoClientSettings object
val settings =
val client = MongoClient.create[IO](settings)

Creating a client through any of the available constructor methods in its companion object returns a Resource[F, MongoClient[F]], meaning that the connection to the MongoDB server will be disposed after its use.

Once the client is created, it can further be used for interacting with MongoDatabase[F] instances that provide methods for dealing with your actual MongoDB database:

import mongo4cats.database.MongoDatabase

MongoClient.fromConnectionString[IO]("mongodb://localhost:27017").use { client =>
  val database: IO[MongoDatabase[IO]] = client.getDatabase("mydb")

If the database does not exist, MongoDB will create it during the very first query to it.