1

Suppose I want to do the following (in mongo shell):

var bulk = db.vectors.initializeOrderedBulkOp()

bulk.find({"_id" : ObjectId("53f265da13d3f885ed8bf75d")}).updateOne({"$pop": {"v": 1}})

bulk.find({"_id" : ObjectId("53f265da13d3f885ed8bf75d")}).updateOne({"$push": {"v": 5}})

bulk.execute()
Stennie
  • 63,885
  • 14
  • 149
  • 175
Lucas Batistussi
  • 2,283
  • 3
  • 27
  • 35
  • 1
    Following answers are deprecated as ReactiveMongo now supports bulk for the write operations: https://stackoverflow.com/a/64258360/3347384 – cchantep Oct 08 '20 at 08:12

2 Answers2

7

I found the answer! ReactiveMongo has RawCommand command that let us run any MongoDB command (like update, in this case >> http://docs.mongodb.org/manual/reference/command/update/#dbcmd.update):

  val commandDoc =
        BSONDocument(
          "update" -> COLLECTION,
          "updates" -> BSONArray(
            BSONDocument("q" -> <query>, "u" -> BSONDocument("$pop" -> BSONDocument("v" -> 1))),
            BSONDocument("q" -> <query>, "u" -> BSONDocument("$push" -> BSONDocument("v" -> 5)))
          ),
          "ordered" -> true
        )

      // we get a Future[BSONDocument]
      val futureResult = db.command(RawCommand(commandDoc))

      futureResult.map { result => // result is a BSONDocument
           //...
      }
Lucas Batistussi
  • 2,283
  • 3
  • 27
  • 35
2

I am using reactive-mongo 0.11.9:

import collection.BatchCommands._
import UpdateCommand._ 
import reactivemongo.bson._

collection.runCommand(Update(
  UpdateElement(q = document(...), u = document(...)), 
  UpdateElement(q = document(...), u = document(...))...
))
Yardena
  • 2,837
  • 20
  • 17