Docs Menu

Docs HomeMongoDB Drivers

MongoDB Scala Driver

On this page

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

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.

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 Stable API feature starting with MongoDB Server version 5.0 and Scala Driver version 4.3. When you use the Stable API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.

Note

Starting from Feburary 2022, the Versioned API is known the Stable API. All concepts and features remain the same with this naming change.

To use this feature, construct a MongoDB client instance, specifying a version of the Stable 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 Stable API version 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.

The following compatibility table specifies the recommended version(s) of the Mongo Scala driver for use with a specific version of MongoDB.

The first column lists the driver version(s).

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 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.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.

The following compatibility table specifies the recommended version(s) of the Mongo Scala driver for use with a specific version of Scala.

The first column lists the driver version(s).

Scala Driver Version
Scala 2.13
Scala 2.12
Scala 2.11
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.

←  MongoDB Rust DriverMongoDB Swift Driver →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.