Search code examples
mongodbreactivemongoplay-reactivemongo

Update with Raw Command in ReactiveMongo 0.12.5


I am not currently able to run a Raw Command in ReactiveMongo 0.12.5 using the Play JSON Plugin. The documentation (Run a raw command) is not currently accessible but from a cached page in my browser I can see the following:

   import scala.concurrent.{ ExecutionContext, Future }
   import play.api.libs.json.{ JsObject, Json }

   import reactivemongo.play.json._
   import reactivemongo.api.commands.Command

   def rawResult(db: reactivemongo.api.DefaultDB)(implicit ec: ExecutionContext): Future[JsObject] = {
     val commandDoc = Json.obj(
       "aggregate" -> "orders", // we aggregate on collection `orders`
       "pipeline" -> List(
         Json.obj("$match" -> Json.obj("status" -> "A")),
         Json.obj(
           "$group" -> Json.obj(
            "_id" -> "$cust_id",
             "total" -> Json.obj("$sum" -> "$amount"))),
         Json.obj("$sort" -> Json.obj("total" -> -1))
       )
     )
     val runner = Command.run(JSONSerializationPack) // run is since deprecated

     runner.apply(db, runner.rawCommand(commandDoc)).one[JsObject] // one is since deprecated
   }

However I am not looking to return a JsObject (or anything in fact) - I actually want to update all documents in another collection as this previous answer illustrates. My issue is that both methods contain deprecated functions and so I have put together a combination to (possibly) work with JSON Collections (as mentioned):

  def bulkUpdateScoreBA(scoreBAs: List[ScoreBA]) = {

    def singleUpdate(scoreBA: ScoreBA) = Json.obj(
      ("q" -> Json.obj("_id" ->
        Json.obj("$oid" -> scoreBA.idAsString(scoreBA._id))
      )),
      ("u" ->
        Json.obj("$set" ->
          Json.obj("scoreBA" -> scoreBA.scoreBA)
        )
      )
    )

    val commandJson = Json.obj(
      "update" -> "rst",
      "updates" -> Json.arr(scoreBAs.map(singleUpdate)),
      "ordered" -> false,
      "writeConcern" -> Json.obj("w" -> "majority", "wtimeout" -> 5000)
    )

    val runner = Command.CommandWithPackRunner(JSONSerializationPack)

    runner.apply(db, runner.rawCommand(commandJson)) // ?? how to get a Future[Unit] here

  }

However I need this to return a Future[Unit] so that I can call it from the controller but I cannot find how this is done or even if what I have done so far is the best way. Any help is appreciated!


Solution

  • The Scaladoc for bulk update is available (since 0.12.7), with example in tests.