ReactiveMongo FindAndModify Clarification

自作多情 提交于 2019-12-06 04:50:13

You can have a look at .findAndUpdate and to FindAndModifyResult which provides a .result operation to get the result according available BSON readers.

val person: Future[Option[AType]] = collection.findAndUpdate(
  BSONDocument("name" -> "James"),
  BSONDocument("$set" -> BSONDocument("age" -> 17)),
  fetchNewObject = true).map(_.result[AType])

I just spent some time looking for play-reactivemongo equivalent . Maybe this example help somebody in future.

val collectionFut: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection[JSONCollection]("sale.numberingCounter"))

def updateIdx(nsId: NumberingSeriesId, month: Option[Int], year: Option[Int], value: Int): Future[Option[NumberingCounter]] = {
 val selector = Json.obj("numberingSeriesId" -> nsId, "month" -> month, "year" -> year)        
 val modifier = Json.obj("$set" -> Json.obj("idx" -> value))
    for {
      collection <- collectionFut
      writeResult <- collection.findAndUpdate(selector,modifier,upsert = true)
    } yield {
      writeResult.value.map(_.as[NumberingCounter])
    }
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!