I am trying to print results from a MongoDB query in Scala
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("tableScala")
val collection: MongoCollection[Document] = database.getCollection("tableScala")
collection.find().printResults()
The error thrown was : Cannot resolve symbol printResults
. Answers to some other questions suggested to use mongo-scala-driver
version 1.2
, as printResults() is not implemented for version 1.1
and below
SBT file:
name := "scalaMongoDriver"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "1.2.0-beta1"
Tried to print manually using :
collection.find().subscribe(
(user: Document) => println(user.toJson()), // onNext
(error: Throwable) => println(s"Query failed: ${error.getMessage}"), // onError
() => println("Done") // onComplete
)
resulted in the following info:
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Is there any way to view the retrieved results in console?
You have to include Helpers.scala
file to use the printResults()
function. It is located in their github repository Helpers.scala.
These helper functions waits for the observable to finish before it prints the values.