You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala开发MongoDB CRUD报错:classpath缺失Document相关符号求助

问题:Scala MongoDB CRUD示例编译报错,缺失bson相关符号

我是Scala新手,编写MongoDB CRUD示例时出现编译错误,提示classpath中缺失org.mongodb.scala.bson.package.Document等相关符号。以下是相关代码、配置、报错日志及仓库信息:

Scala代码

package com.example
import org.mongodb.scala.{Document, MongoClient, MongoCollection, MongoDatabase}
object App {
  def main(args: Array[String]): Unit = {
    val client: MongoClient = MongoClient()
    val database: MongoDatabase = client.getDatabase("latest_db")
    val collection: MongoCollection[Document] = database.getCollection("students")
    // insert a document
    val document: Document = Document("name" -> "John", "age" -> 51)
    val insertObservable = collection.insertOne(document)
    println("Document inserted")
    // find documents
    collection.find()
      .collect()
      .subscribe((results: Seq[Document]) => println(s"Found: #${results.size}"))
  }
}

build.gradle配置

dependencies {
    implementation group: 'org.mongodb.scala', name: 'mongo-scala-driver_2.13', version: '4.10.2'
    implementation group: 'org.mongodb', name: 'mongodb-driver-reactivestreams', version: '4.10.2'
    implementation 'org.scala-lang:scala-library:2.13.10'
    implementation 'com.google.guava:guava:31.1-jre'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.scalatest:scalatest_2.13:3.2.15'
    testImplementation 'org.scalatestplus:junit-4-13_2.13:3.2.2.0'
    testRuntimeOnly 'org.scala-lang.modules:scala-xml_2.13:1.2.0'
}

报错日志

> Task :app:compileKotlin NO-SOURCE
> Task :app:compileJava NO-SOURCE
> Task :app:compileScala
[Error] /app/src/main/scala/com/example/App.scala:13:37: Symbol 'type org.mongodb.scala.bson.package.Document' is missing from the classpath.
This symbol is required by 'type org.mongodb.scala.Document'.
Make sure that type Document is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
A full rebuild may help if 'package.class' was compiled against an incompatible version of org.mongodb.scala.bson.package.
[Error] /app/src/main/scala/com/example/App.scala:13:49: Symbol 'term org.mongodb.scala.bson.DefaultHelper' is missing from the classpath.
This symbol is required by 'value org.mongodb.scala.MongoDatabase.e'.
Make sure that term DefaultHelper is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
A full rebuild may help if 'MongoDatabase.class' was compiled against an incompatible version of org.mongodb.scala.bson.
[Error] /app/src/main/scala/com/example/App.scala:16:30: Symbol 'term org.mongodb.scala.bson.collection.immutable.Document' is missing from the classpath.
This symbol is required by 'value org.mongodb.scala.Document'.
Make sure that term Document is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
A full rebuild may help if 'package.class' was compiled against an incompatible version of org.mongodb.scala.bson.collection.immutable.
[Error] /app/src/main/scala/com/example/App.scala:16:38: <notype> does not take parameters
[Error] /app/src/main/scala/com/example/App.scala:21:5: Symbol 'term org.mongodb.scala.bson.conversions.package' is missing from the classpath.
This symbol is required by 'value org.mongodb.scala.MongoCollection.filter'.
Make sure that term package is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
A full rebuild may help if 'MongoCollection.class' was compiled against an incompatible version of org.mongodb.scala.bson.conversions.
5 errors found
> Task :app:compileScala FAILED
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.2.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
1 actionable task: 1 executed
FAILURE: Build failed with an exception.

仓库地址:https://github.com/ravisuthar/My-Gradle-Scala-MongoDB


解决方案

1. 移除重复依赖,确保驱动包完整

mongo-scala-driver已经包含了mongodb-driver-reactivestreams的依赖,无需单独声明,重复引入可能导致依赖冲突。修改build.gradle的dependencies块:

dependencies {
    implementation group: 'org.mongodb.scala', name: 'mongo-scala-driver_2.13', version: '4.10.2'
    implementation 'org.scala-lang:scala-library:2.13.10'
    implementation 'com.google.guava:guava:31.1-jre'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.scalatest:scalatest_2.13:3.2.15'
    testImplementation 'org.scalatestplus:junit-4-13_2.13:3.2.2.0'
    testRuntimeOnly 'org.scala-lang.modules:scala-xml_2.13:1.2.0'
}

2. 清理并重新构建项目

执行以下Gradle命令,清理旧的编译缓存和依赖,重新下载并构建:

./gradlew clean build --refresh-dependencies

3. 验证依赖传递

如果问题仍存在,检查mongo-scala-driver是否正确传递了mongo-scala-bson包。可以通过./gradlew dependencies命令查看依赖树,确认org.mongodb.scala:mongo-scala-bson_2.13:4.10.2是否存在于依赖列表中。

补充说明

Scala驱动中的Document类实际来自org.mongodb.scala.bson.collection.immutable.Document,而org.mongodb.scala.Document是它的别名。依赖缺失会导致别名无法解析,进而引发编译错误。确保驱动包完整且无版本冲突是解决问题的核心。

内容的提问来源于stack exchange,提问作者2787184

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 22:05:26