mongo-scala-driver: Getting Results

1.7k Views Asked by At

I have the following document in Mongodb:

{ "index_column" : "site_id", "mapped_column":"site_name"}

I'm using the mongo-scala-driver from my scala code to query the server. How do I get the mapped_column to a scala String variable? I tried with the following code:

val result = mongocollection.find(equal("index_column", data)).first()

It returns a org.mongodb.scala.ObservableImplicits$BoxedObservable@207c9b87 Is it possible to convert the Observable to an Iterator? I just need the mapped_column value in a scala String variable.

2

There are 2 best solutions below

0
On

I resolved this issue by switching the driver to mongodb casbah and it can be done with the following code:

val result = mongocollection.find(MongoDBObject("index_column"-> "site_id")).one()

println(result.get("mapped_column"))
0
On

Scala driver is completely asynchronous, so you can subscribe for the result. The simplest way is to use foreach:

val result = mongocollection.find(equal("index_column", data)).first()
result.foreach { value => ... }

Another option is to convert Observable to Future:

Await.result(result.toFuture(), 3 seconds)