I am searching for the right way to implement the upsert operation to a Mongo Collection using the API given by Spring Data.
In details, I have the following u
I believe what you are looking for is $setOnInsert
for subdocument1
. So something like should work for you.
Query query = Query.query(Criteria.where("_id").is("some_id"));
Update update = Update.update("_id", "some_id")
.set("field1", "value1")
.set("field2", "value2")
.set("subdocument2", subdocumentObject2)
.setOnInsert("subdocument1", subdocumentObject1);
More here https://docs.mongodb.com/manual/reference/operator/update/setOnInsert/