有没有办法在ReactiveMongo上执行批量更新?

Luc*_*ssi 1 scala reactivemongo

假设我想要执行以下操作(在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()
Run Code Online (Sandbox Code Playgroud)

Luc*_*ssi 5

我找到了答案!ReactiveMongo具有RawCommand命令,允许我们运行任何MongoDB命令(如更新,在本例中为>> 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
           //...
      }
Run Code Online (Sandbox Code Playgroud)