Docs 菜单

Docs 主页开发应用程序MongoDB 驱动程序

MongoDB Scala 驱动程序

在此页面上

  • 简介
  • 安装
  • 连接到 MongoDB Atlas
  • 在没有 Stable API 的情况下连接到 MongoDB Atlas
  • 连接到本地计算机上的 MongoDB Server
  • 兼容性

欢迎访问官方 MongoDB Scala 驱动程序的文档站点。您可以将驱动程序添加到应用程序中,以与 Scala 中的 MongoDB 异步工作。使用 sbt 下载 或 Apache Maven ,或按照我们的教程设置可运行的项目。

开始在项目中使用驱动程序的推荐方法是使用依赖项管理系统,例如sbtmaven 。请参阅 安装指南 以了解更多信息。

您可以使用以下连接片段来测试与 Atlas 上 MongoDB 部署的连接:

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!")
}
}
}

此连接代码段使用 Stable API 功能,可以在使用 Scala 驱动程序 v4.3 及更高版本连接到 MongoDB Server v5.0 及更高版本时启用该功能。使用此功能时,可以更新驱动程序或服务器,而不必担心 Stable API 涵盖的任何命令的向后兼容性问题。

要了解有关 Stable API 功能的更多信息,请参阅服务器手册中的Stable API

注意

从 2022 年 2 月开始,版本化 API被称为 Stable API。此次命名更改后,所有概念和功能均保持不变。

如果您使用的 MongoDB 版本或驱动程序不支持“稳定 API”功能,您可以使用以下代码片段来测试与 Atlas 上的 MongoDB 部署的连接:

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!")
}
}
}

如果您需在本地机器上运行 MongoDB 服务器以用于开发目的而不是使用 Atlas 集群,则需完成以下操作:

  1. 下载CommunityEnterprise版本的 MongoDB Server。

  2. 安装和配置MongoDB Server。

  3. 启动该服务器。

重要

始终保护您的 MongoDB 服务器免受恶意攻击。请参阅我们的安全检查清单以获取安全建议列表。

在成功启动 MongoDB 服务器后,在驱动程序连接代码中指定连接字符串。

如果 MongoDB Server 在本地运行,您可以使用连接字符串 "mongodb://localhost:<port>",其中 <port> 是您配置服务器以侦听传入连接的端口号。

如果需要指定不同的主机名或 IP 地址,请参阅我们的《服务器手册》中有关连接字符串的条目。

要测试是否可以连接到服务器,请替换“连接到 MongoDB Atlas”代码示例中的连接字符串并运行。

如下兼容性表指定了推荐与特定版本的 MongoDB 结合使用的 Mongo Scala 驱动程序的版本。

第一列列出驱动程序版本。

重要

在服务器版本生命周期结束 (EOL) 日期后的三年内,MongoDB 确保 MongoDB Server 和驱动程序之间的兼容性。要了解有关 MongoDB 版本和 EOL 日期的更多信息,请参阅MongoDB 软件生命周期时间表。

图标
解释
支持所有功能。
该驱动程序版本将与 MongoDB 版本一起使用,但并不支持所有新的 MongoDB 功能。
无标记
驱动程序版本未使用 MongoDB 版本测试。
Scala 驱动程序版本
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.11
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

此驱动程序不支持早期版本的 MongoDB。

以下兼容性表格指定了与特定版本的 Scala 一起使用的推荐版本或多个 Mongo Scala 驱动程序版本。

第一列列出驱动程序版本。

Scala 驱动程序版本
Scala 2.13
Scala 2.12
Scala 2.11
4.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

有关如何阅读兼容性表的更多信息,请参阅我们的 MongoDB 兼容性表指南。

← MongoDB Ruby 驱动程序