简体   繁体   中英

Printing query results from Mongodb in Scala using mongo-scala-driver

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.

I had the same problem today, but I found a solution on the web.

The way that worked for me was adding a Thread.sleep in the end of the code, so the program end after the asynchronous call has the chance to print the elements.

This happens because of the reactive nature of Mongo's Observables, meaning that you have to do most of your operations making the use of Futures.

Just the Thread.sleep should work.

Hope it helps!

MongoDB driver is asynchronous, to make it synchronouse you may apply some of the monads operators

  1. andThen : Allows the chaining of Observables. collect : Collects all the results into a sequence.
  2. flatMap : Create a new Observable by applying a function to each result of the Observable.
  3. foldLeft : Creates a new Observable that contains the single result of the applied accumulator function. foreach : Applies a function applied to each emitted result.
  4. head : Returns the head of the Observable in a Future.
  5. map : Creates a new Observable by applying a function to each emitted result of the Observable.

Full list of observables :

For example:

val doc = Await.result(myCollection
          .find(and(regex("date", s"^${date}T.*"), equal("field", fieldValue)))
          .sort(descending("timestamp"))
          .first().head(), Duration(10, SECONDS))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM