在 ReactiveMongo 0.18.8 中,如何在单个命令中更新多个具有不同值的文档? [英] In ReactiveMongo 0.18.8, how can I perform an update of several documents, with different values, in a single command?

查看:43
本文介绍了在 ReactiveMongo 0.18.8 中,如何在单个命令中更新多个具有不同值的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个答案挖掘 SO.

基本上,这正是我需要的.但是,在我当前的 ReactiveMongo 版本 0.18.8 中,db.command(RawCommand(commandDoc)) 似乎不再可能.没有命令"在 DB 下.我似乎无法找到此命令移动的位置.

谁能帮帮我?或者告诉我我还能如何实现我需要的东西?

我正在尝试在单个 DB 命令中对具有不同值的多个文档执行多次更新.

谢谢!

解决方案

文档 您可以查看插入、更新或删除的批量操作示例.

import scala.concurrent.Future导入 scala.concurrent.ExecutionContext.Implicits.global导入reactivemongo.api.bson.BSONDocument导入reactivemongo.api.bson.collection.BSONCollectiondef updateWithBulk(personCol: BSONCollection) = {//批量更新:多次更新val updateBuilder1 = personColl.update(ordered = true)val 更新 = Future.sequence(Seq(updateBuilder1.element(q = BSONDocument("firstName" -> "Jane", "lastName" -> "Doh"),u = BSONDocument(年龄"-> 18),upsert = 真,多 = 假),updateBuilder1.element(q = BSONDocument("firstName" -> "Bob"),u = BSONDocument(年龄"-> 19),插入 = 假,多 = 真))))updates.flatMap { ops =>updateBuilder1.many(ops) }}

<块引用>

另请注意,0.18.8 版本大约有一年的历史,最新版本是主要版本 1.0.0.

I found this answer whilst digging in SO.

Basically, this is exactly what I need. However, it appears that in my current version of ReactiveMongo, which is 0.18.8, db.command(RawCommand(commandDoc)) is no longer possible. There is no "command" under DB. I can't seem to find where this command moved.

Can anyone please help me? Or maybe tell me how else I can implement what I need?

I am trying to perform several updates, of several documents with different values, in a single DB command.

Thanks!

解决方案

In the documentation you can see example of bulk operations to insert, update or delete.

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import reactivemongo.api.bson.BSONDocument

import reactivemongo.api.bson.collection.BSONCollection

def updateWithBulk(personColl: BSONCollection) = {
  // Bulk update: multiple update
  val updateBuilder1 = personColl.update(ordered = true)
  val updates = Future.sequence(Seq(
    updateBuilder1.element(
      q = BSONDocument("firstName" -> "Jane", "lastName" -> "Doh"),
      u = BSONDocument("age" -> 18),
      upsert = true,
      multi = false),
    updateBuilder1.element(
      q = BSONDocument("firstName" -> "Bob"),
      u = BSONDocument("age" -> 19),
      upsert = false,
      multi = true)))

  updates.flatMap { ops => updateBuilder1.many(ops) }
}

Also note that version 0.18.8 is about one year old, latest version being the major release 1.0.0.

这篇关于在 ReactiveMongo 0.18.8 中,如何在单个命令中更新多个具有不同值的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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