ReactiveMongo: How to convert BSON returned by FindAndModify to JSON

≡放荡痞女 提交于 2019-12-04 16:37:08
viktortnk

BSON handlers implicits (suggested in comment) might not work because FindAndModify command has a strict signature to return Option[BSONDocument]

FindAndModify extends BSONCommandResultMaker[Option[BSONDocument]]

given the returned result is of Future[Option[BSONDocument]] type

you can import the json formats

import play.modules.reactivemongo.json.BSONFormats._

and apply

result.map(docOpt => docOpt.map(d => Json.toJson(d)))

on result, or call the conversion directly

import play.modules.reactivemongo.json.BSONFormats

result.map(docOpt => docOpt.map(d =>
  BSONFormats.BSONDocumentFormat.writes(d).as[JsObject]))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!