0

Is there any way to update bulk records. I am trying to update user object using following code:

.update($doc("_id" $in (usersIds: _*)), users, GetLastError(), false , true)

In above code i am passing, users List. in user list i also add new properties and chage existing properties state, but with this statement the records are not update

If i am using following code:

.update($doc("_id" $in (usersIds: _*)), $set("inviteStatus" $eq "Invited"), GetLastError(), false , true)

The record updated successfully.

Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126

1 Answers1

0

From my reading of the API as well as other answers in this area, I don't think the kind of bulk update you're looking for is possible via the update method.

What you could do however, is issue a Raw Command (credit: this answer), which, if you are patient enough to write the all the $set expressions, would definitely work, and be more efficient than a client-side loop doing save() operations one-by-one:

Caution: compiles, but is untested:

import reactivemongo.api.DB
import reactivemongo.bson._
import reactivemongo.core.commands.RawCommand

class BulkUpdater(db:DB) {

  def bulkUpdateUsers(users:List[User]) = {

    def singleUpdate(u:User) = BSONDocument (
      ("q" -> BSONDocument("_id" -> u._id)),
      ("u" -> BSONDocument("$set" -> BSONDocument(
        "firstName" -> u.firstName, 
        "secondName" -> u.secondName) // etc
    )))

  val commandBson = BSONDocument(
    "update" -> "users",
    "updates" -> BSONArray(users.map(singleUpdate)),
    "ordered" -> false,
    "writeConcern" -> BSONDocument( "w" -> "majority", "wtimeout" -> 5000)
  )

  db.command(RawCommand(commandBson))
}
Community
  • 1
  • 1
millhouse
  • 9,817
  • 4
  • 32
  • 40
  • Hello @millhouse, is there a way to run `RawCommand` for `Json` object ? – Harmeet Singh Taara May 22 '15 at 06:04
  • It should be possible to define the body of the `RawCommand` as a Play `JsValue` and then convert it to a `BSONDocument` using the [BSONFormats](https://github.com/ReactiveMongo/Play-ReactiveMongo/blob/master/src/main/scala/play/modules/reactivemongo/json.scala) from the Play ReactiveMongo library. – millhouse May 22 '15 at 06:58
  • Hey @millhouse, this is not work for me. Following is my code: ` val commandJSON = BSONDocument( "udapte" -> "users", "updates" -> BSONArray(users.map {userUpdateQueryDocument}), "ordered" -> false, "writeConcern" -> BSONDocument( "w" -> "majority") )` – Harmeet Singh Taara May 22 '15 at 07:53
  • When i execute this code ` val doc = userCollection.db.command(RawCommand(commandJSON)) doc.map { bson => println(">>>>>>>>>>>>>>>>>>>>>>>>: "+bson) } ` The println statement not run – Harmeet Singh Taara May 22 '15 at 08:06
  • my code through following exception : ` reactivemongo.core.commands.DefaultCommandError: BSONCommandError['command faile d because the 'ok' field is missing or equals 0'] with original doc { ` – Harmeet Singh Taara May 22 '15 at 08:09
  • My error message is `errmsg: BSONString(no such cmd: udapte)` . – Harmeet Singh Taara May 22 '15 at 09:40
  • Check a) that you spelled `update` correctly in the command and b) that you are running Mongo 2.6 or later (`update` first appeared in 2.6) – millhouse May 23 '15 at 04:00
  • hello @ millhouse i am using mongodb `2.6.7` version – Harmeet Singh Taara May 25 '15 at 08:01