我是 MongoDB 和 Scala 语言的新手
我正在使用scala语言在本地连接mongodb
我正在使用以下依赖项
// https://mvnrepository.com/artifact/org.mongodb.scala/mongo-scala-driver
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.2.3"
我试过的
object Demo extends App {
val mongoClient: MongoClient = MongoClient("mongodb://127.0.0.1:27017/")
val database: MongoDatabase = mongoClient.getDatabase("DemoDB")
println(database)
val collection: MongoCollection[Document] =database.getCollection("demodata");
val observable = collection.find();
}
上面的代码以以下格式返回数据
FindObservable(com.mongodb.reactivestreams.client.internal.FindPublisherImpl@6253c26)
我也试过
observable.subscribe ( new Observer[Document] {
override def onNext(result: Document): Unit = println(result.toJson())
override def onError(e: Throwable): Unit = println("Failed" + e.getMessage)
override def onComplete(): Unit = println("Completed")
})
我也尝试过printResult()
方法printHeadResult()
,但没有一种方法有效
请帮助提前谢谢