FindById return JSON object

19 views
Skip to first unread message

Kylian Deau

unread,
Sep 9, 2016, 5:20:06 AM9/9/16
to ReactiveMongo - http://reactivemongo.org
I want a method in my controller to retrieve the JSON representation of a MongoDB document, and it takes as parameter the ObjectID of the document in the collection "descriptions" of my db.

def descriptionsFuture: Future[JSONCollection] = database.map(_.collection[JSONCollection]("descriptions"))

Here is my method (incomplete) :

def view(id:String) = Action {
val mongo_find_query = Json.obj("_id" -> Json.obj("$oid" ->id))
val resultFuture: Future[Option[JsObject]] = descriptionsFuture.flatMap { descriptions =>
descriptions.find(mongo_find_query).one[JsObject]
}

Ok(Json.prettyPrint(Json.toJson(descriptions)))
}

How can i return the JSON of my document ? I do not really understand how to handle the resultFuture. If the document does not exist, i want to return an error via HTTP.

Thanks

Cédric Chantepie

unread,
Sep 9, 2016, 7:38:08 AM9/9/16
to ReactiveMongo - http://reactivemongo.org
Hi, there is nothing specific to ReactiveMongo about that. I suggest you have a look at the Play documentation https://www.playframework.com/documentation/2.5.x/ScalaAsync
Reply all
Reply to author
Forward
0 new messages