Is there any way to perform bulk updates on ReactiveMongo?

落花浮王杯 提交于 2020-02-06 03:50:27

问题


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()

回答1:


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
           //...
      }



回答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(...))...
))


来源:https://stackoverflow.com/questions/25372398/is-there-any-way-to-perform-bulk-updates-on-reactivemongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!