Question

I'm trying to include the $setOnInsert field for Update and FindAndModify in ReactiveMongo. However, I can't seem to get it to work with the built in Commands.

Does anyone know how to include this field for updating a document?

Was it helpful?

Solution

This simple example works for me:

val collection = ... // your collection goes here
collection.update(
  obj("_id" -> "1"),
  obj(
    "$setOnInsert" -> obj("field 1" -> "value 1"), 
    "$set" -> obj("field 2" -> "value 2")),
  upsert = true
).map {
  case n => println(n.ok.toString) // do smth
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top