使用mongo-scala驱动程序在Scala中打印MongoDB的查询结果 [英] Printing query results from Mongodb in Scala using mongo-scala-driver
本文介绍了使用mongo-scala驱动程序在Scala中打印MongoDB的查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Scala中打印MongoDB查询的结果
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("tableScala")
val collection: MongoCollection[Document] = database.getCollection("tableScala")
collection.find().printResults()
引发的错误为:Cannot resolve symbol printResults
。其他一些问题的答案建议使用mongo-scala-driver
版本1.2
,因为1.1
和更低版本没有实现printResults()
SBT文件:
name := "scalaMongoDriver"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "1.2.0-beta1"
尝试使用以下工具手动打印:
collection.find().subscribe(
(user: Document) => println(user.toJson()), // onNext
(error: Throwable) => println(s"Query failed: ${error.getMessage}"), // onError
() => println("Done") // onComplete
)
导致以下信息:
信息:未选择服务器 来自群集的ReadPreferenceServerSelector{readPreference=primary} Description ClusterDescription{类型=未知,连接模式=单一, ServerDescriptions=[ServerDescription{address=localhost:27017, 类型=未知,状态=正在连接}]}。在计时前等待30000毫秒 输出
是否可以在控制台中查看检索到的结果?
推荐答案
您必须包含Helpers.scala
文件才能使用printResults()
函数。它位于他们的GitHub存储库中Helpers.scala。
这些帮助器函数等待可观察对象完成,然后再打印值。
这篇关于使用mongo-scala驱动程序在Scala中打印MongoDB的查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文