阅读mongo-scala-driver_2.12
的文档,更具体地说是阅读MongoCollection的文档,我认为我应该能够执行以下操作来连接到mongo更改流
val collection: MongoCollection[Document] = database.getCollection("MyCollection")
collection.watch().subscribe(
(doc: Document) => println(doc.toJson),
(t: Throwable) => // handle failure,
() => // handle this case too
)
但是我收到一个编译错误,指出watch()
不是集合的成员。
文档中的方法描述为
def watch[C]()(implicit e: DefaultsTo[C, TResult], ct: ClassTag[C]): ChangeStreamObservable[C]
文档有问题吗?使用
进入quick tour时遇到类似的问题collection.find().first().printHeadResult()
该行将不会编译,表明printHeadResult
不是成员。
作为副节点,我可以使用python连接到mongodb更改流,但是我们想利用scala的多线程功能。
我正在使用mongo-scala-driver_2.12
的Scala 2.12.6版和2.1.0版。
产生此编译错误的最小示例是
import org.mongodb.scala._
/**
* @author ${user.name}
*/
object App {
def main(args : Array[String]) {
val mongoClient: MongoClient = MongoClient("connection-string")
val database: MongoDatabase = mongoClient.getDatabase("db")
val collection: MongoCollection[Document] = database.getCollection("MyCollection")
collection.watch().subscribe(
(doc: Document) => println(doc.toJson),
(t: Throwable) => // handle failure,
() => println("done"))
}
}
整个堆栈跟踪为
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.company:scala >----------------------
[INFO] Building scala 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ scala_tutorial ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory scala/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ scala ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- scala-maven-plugin:3.3.2:compile (default) @ scala ---
[INFO] scala/src/main/scala:-1: info: compiling
[INFO] Compiling 1 source files to scala/target/classes at 1530296517564
[ERROR] scala/src/main/scala/com/company/App.scala:21: error: value watch is not a member of org.mongodb.scala.MongoCollection[org.mongodb.scala.Document]
[ERROR] collection.watch().subscribe(
[ERROR] ^
[ERROR] one error found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.125 s
[INFO] Finished at: 2018-06-29T11:21:59-07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.3.2:compile (default) on project scala_tutorial: wrap: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
答案 0 :(得分:1)
您提供了指向MongoCollection
的{{3}}版本的链接,该链接的确具有watch
方法,但是您正在使用的2.2版本的链接却没有watch
方法。使用Mongo Scala驱动程序的2.2版本(或更高版本;当前版本为2.4)。
至于您在遵循文档中的快速浏览时看到的错误,2.1类中隐式提供了printHeadResult
方法。 Helpers.scala
页面的末尾描述了此帮助程序类。如果要使用它们,则需要导入它们。