2
 val db = mongoClient("test") 
 val coll = db("test")
 val q  = MongoDBObject("id" -> 100) 
 val result= coll.findOne(q)

How can I convert result to a map of key --> value pairs?

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

1

result of findOne is an Option[Map[String, AnyRef]] because MongoDBObject is a Map. A Map is already a collection of pairs. To print them, simply:

for {
   r <- result
   (key,value) <- r
}
yield println(key + " " + value.toString)

or

result.map(_.map({case (k,v) => println(k + " " + v)}))

To serialize mongo result, try com.mongodb.util.JSON.serialize, like

com.mongodb.util.JSON.serialize(result.get)
Bask.ws
  • 823
  • 4
  • 6