Spring Data Mongo: upsert with various fields updated

前端 未结 1 876
滥情空心
滥情空心 2021-01-28 02:05

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

相关标签:
1条回答
  • 2021-01-28 02:36

    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/

    0 讨论(0)
提交回复
热议问题