我正在尝试在 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
version的其他一些问题的答案1.2
,因为 printResults() 未针对 version1.1
及以下版本实现
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} 从集群描述 ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]} 中没有选择服务器。在超时前等待 30000 毫秒
有没有办法在控制台中查看检索到的结果?