How to replicate Mongo update query in Spring Boot

2019-08-17 00:51发布

问题:

I want to replicate a Mongo update query in Spring Boot with Kotlin code.

  • Spring Boot 2.0.6
  • Spring Data MongoDB 2.0.6
  • Mongo Driver 3.6.4
  • Kotlin 1.2.71

I have tried to execute the query through mongoTemplate but no update option allows me to pass it as parameter an object of type UpdateOptions.

Mongo query update to replicate

db.proyectos.update({
   _id: ObjectId("5bfa09f0a0441f38d45dcc9c")
},
{
   $set: {
      "reuniones.$[i].participantes.$[j].firma": "modificar"
   }
},
{
   arrayFilters: [
      {
         "i._id": ObjectId("5bfa09f0a0441f38d45dcc99")
      },
      {
         "j.nomina": 2
      }
   ]
})

MongoTemplate, Query, UpdateOptions and Update

MongoTemplate

var mongoTemplate = MongoTemplate(MongoClient("localhost"), "spring_mongo_db")

Query

val query: Query = Query()
query.addCriteria(Criteria("_id").`is`(firmaRequest.IdProyecto))

UpdateOptions

val updateOptions: UpdateOptions = UpdateOptions()
updateOptions
   .arrayFilters(
      Arrays.asList(
         Filters.eq("i._id", firmaRequest.IdReunion),
         Filters.eq("i.nomina", firmaRequest.NoNomina)
      )
)

Update

val update: Update = Update()
update.set("reuniones.$[i].participantes.$[j].firma", firmaRequest.DataUri)

Expected behavior

I expect to be able to execute the query through mongoTemplate or by another form.