简体   繁体   中英

Using array filters when updating a document with ReactiveMongoDB

Is there a way to use MongoDB's identifier update array filter in ReactiveMongoDB (19.2)? Imagine a db collection schema of:

{
    "_id": ObjectId("617942b33b850d6bbc490420"),
    "provider": "Google",
    "myArr_e": [
        {
            "name": "cloud",
            "lastDate": ISODate("2021-10-27T18:00:00.000+00:00")
        }
    ]
}

I want to update lastDate field where the document id is ObjectId("617942b33b850d6bbc490420") and the myArr_e.name is "cloud". I can successfully compose the query in MongoDB with the following:

db.apv.updateOne({ "_id": ObjectId("617942b33b850d6bbc490420") }, { $set: { "myArr_e.$[elem].lastDate": new Date() }}, { "arrayFilters": [{ "elem.name": "cloud" }]});

But I cannot do this in my app with ReactiveMongoDB. This was my best attempt:

  def getCollection(collName: String): Future[JSONCollection]

  def updateMyArr: Future[WriteResult] = {

    val q = obj("_id" -> obj("$oid" -> "617942b33b850d6bbc490420"))
    val u = obj("$set" -> obj("myArr_e.$[elem].lastDate" -> new Date()))
    val af = obj("arrayFilters" -> arr(obj("elem.name" -> "cloud")))

    for {
      coll <- getCollection("collName")
      uwr <- coll.update.one(q, u, upsert = false, multi = false, collation = None, arrayFilters = af)
    } yield {
      uwr
    }

  }

I'm getting a http 202 from this but I can see that the document hasn't updated. Is this possible in ReactiveMongoDB (19.2)? I've tried to find documentation on this but if it's out there then it's hiding from me. With thanks_

This is actually editing the document correctly this morning so this must have been me but I wanted to leave it up in case anyone is looking for an example of how to do this. Also just a slight amendment to ensure an ISODate type is written to the field rather than a Long type & per the documentation the smartest way to construct the update object:

val u = obj("$currentDate" -> obj("myArr_e.$[elem].lastDate" -> true))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM