有没有办法在 ReactiveMongo 上执行批量更新? [英] Is there any way to perform bulk updates on ReactiveMongo?

查看:63
本文介绍了有没有办法在 ReactiveMongo 上执行批量更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想执行以下操作(在 mongo shell 中):

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

推荐答案

我找到了答案!ReactiveMongo 有 RawCommand 命令,可以让我们运行任何 MongoDB 命令(比如更新,在这种情况下 >> http://docs.mongodb.org/manual/reference/command/update/#dbcmd.update):

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

这篇关于有没有办法在 ReactiveMongo 上执行批量更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆