Docs Menu

MongoDB Scala Driver

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Versioned API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

This is the officially supported Scala driver for MongoDB.

It's a modern idiomatic Scala driver with asynchronous and non-blocking IO.

The recommended way to get started using the driver in your project is with a dependency management system like sbt or maven. See the Installation Guide for more information.

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

import org.mongodb.scala._
// ...
// Replace the uri string with your MongoDB deployment's connection string.
val uri: String = "mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority"
System.setProperty("org.mongodb.async.type", "netty")
val client: MongoClient = MongoClient(uri)
val db: MongoDatabase = client.getDatabase("test")
Note

For information about connecting to Atlas Serverless, see the Serverless Instance Limitations page for the minimum driver version you need.

See our guide on Connecting for more ways to connect.

You can use the Versioned API feature starting with MongoDB Server version 5.0 and Scala Driver version 4.3. When you use the Versioned API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Versioned API. To use this feature, construct a MongoDB client instance, specifying a version of the Versioned API:

import com.mongodb.{ServerApi, ServerApiVersion}
import org.mongodb.scala._
// ...
// Replace <connection string> with your MongoDB deployment's connection string.
val uri = "<connection string>"
// Set the version of the Versioned API on the client.
val mongoClientSettings = MongoClientSettings.builder().applyConnectionString(ConnectionString(uri))
.serverApi(ServerApi.builder().version(ServerApiVersion.V1).build())
.build()
val mongoClient = MongoClient(mongoClientSettings)

If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:

  1. Download the Community or Enterprise version of MongoDB Server.
  2. Install and configure MongoDB Server.
  3. Start the server.
Important

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.

After you successfully start your MongoDB server, specify your connection string in your driver connection code.

If your MongoDB Server is running locally, you can use the connection string "mongodb://localhost:<port>" where <port> is the port number you configured your server to listen for incoming connections.

If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.

To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

Scala Driver Version
MongoDB 5.1
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6
4.2

4.1

2.9

2.8

2.7

2.6

2.5

2.4

2.3

2.2

2.1

2.0

1.2

1.1

1.0

The driver does not support older versions of MongoDB.

Scala Driver Version
Scala 2.13
Scala 2.12
Scala 2.11
4.2

4.1

2.9

2.8

2.7

2.6

2.5

2.4

2.3

2.2

2.1

2.0

1.1

1.1

1.0

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.