MongoDB Scala Driver
On this page
Introduction
Welcome to the documentation site for the official MongoDB Scala driver. You can add the driver to your application to work asynchronously with MongoDB in Scala. Download it using sbt or Apache Maven, or set up a runnable project by following our tutorials.
Installation
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.
Connect to MongoDB Atlas
You can use the following connection snippet to test your connection to your MongoDB deployment on Atlas:
import com.mongodb.{ServerApi, ServerApiVersion} import org.mongodb.scala.{ConnectionString, MongoClient, MongoClientSettings} import org.mongodb.scala.bson.Document import scala.concurrent.Await import scala.concurrent.duration.DurationInt import scala.util.Using object MongoClientConnectionExample { def main(args: Array[String]): Unit = { // Replace the placeholder with your Atlas connection string val connectionString = "<connection string>"; // Construct a ServerApi instance using the ServerApi.builder() method val serverApi = ServerApi.builder.version(ServerApiVersion.V1).build() val settings = MongoClientSettings .builder() .applyConnectionString(ConnectionString(connectionString)) .serverApi(serverApi) .build() // Create a new client and connect to the server Using(MongoClient(settings)) { mongoClient => // Send a ping to confirm a successful connection val database = mongoClient.getDatabase("admin") val ping = database.runCommand(Document("ping" -> 1)).head() Await.result(ping, 10.seconds) System.out.println("Pinged your deployment. You successfully connected to MongoDB!") } } }
This connection snippet uses the Stable API feature, which you can enable when using the Scala driver v4.3 and later to connect to MongoDB Server v5.0 and later. When you use this feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.
To learn more about the Stable API feature, see Stable API in the Server manual.
Note
Starting from Feburary 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.
Connect to MongoDB Atlas Without the Stable API
If you are using a version of MongoDB or the driver that doesn't support the Stable API feature, you can use the following code snippet to test your connection to your MongoDB deployment on Atlas:
import org.mongodb.scala.MongoClient import org.mongodb.scala.bson.Document import scala.concurrent.Await import scala.concurrent.duration.DurationInt import scala.util.Using object MongoClientConnectionExample { def main(args: Array[String]): Unit = { // Replace the placeholder with your Atlas connection string val connectionString = "<connection string>"; // Create a new client and connect to the server Using(MongoClient(connectionString)) { mongoClient => // Send a ping to confirm a successful connection val database = mongoClient.getDatabase("admin") val ping = database.runCommand(Document("ping" -> 1)).head() Await.result(ping, 10.seconds) System.out.println("Pinged your deployment. You successfully connected to MongoDB!") } } }
Connect to a MongoDB Server on Your Local Machine
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:
Download the Community or Enterprise version of MongoDB Server.
Install and configure MongoDB Server.
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.
Compatibility
MongoDB Compatibility
The following compatibility table specifies the recommended version or versions of the Mongo Scala driver for use with a specific version of MongoDB.
The first column lists the driver version.
Important
MongoDB ensures compatibility between the MongoDB Server and the drivers for three years after the server version's end of life (EOL) date. To learn more about the MongoDB release and EOL dates, see MongoDB Software Lifecycle Schedules.
Compatibility Table Legend
Icon | Explanation |
---|---|
✓ | All features are supported. |
⊛ | The Driver version will work with the MongoDB version, but not all
new MongoDB features are supported. |
No mark | The Driver version is not tested with the MongoDB version. |
Scala Driver Version | MongoDB 7.0 | MongoDB 6.1 | MongoDB 6.0 | 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.10 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.9 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.8 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.7 | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.6 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.5 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.4 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.3 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.2 | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.1 | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.0 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
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.
Language Compatibility
The following compatibility table specifies the recommended version or versions of the Mongo Scala driver for use with a specific version of Scala.
The first column lists the driver version.
Scala Driver Version | Scala 2.13 | Scala 2.12 | Scala 2.11 |
---|---|---|---|
4.10 | ✓ | ✓ | ✓ |
4.9 | ✓ | ✓ | ✓ |
4.8 | ✓ | ✓ | ✓ |
4.7 | ✓ | ✓ | ✓ |
4.6 | ✓ | ✓ | ✓ |
4.5 | ✓ | ✓ | ✓ |
4.4 | ✓ | ✓ | ✓ |
4.3 | ✓ | ✓ | ✓ |
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 | ✓ |
For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.
How to get help
Ask questions on our MongoDB Community Forums.
Visit our Support Channels.
See Issues & Help.