简体   繁体   English

如何使用 Spring Boot 更新 MongoDB 集合中的子对象?

[英]How to update a sub-object in a MongoDB collection with Spring Boot?

I'm new to both Spring Data and StackOverflow.我是 Spring Data 和 StackOverflow 的新手。

To make it simple, in my database I have a 'user' collection in which I have a user document with a 'myApp' subobject and a 'param' subobject.为简单起见,在我的数据库中,我有一个“用户”集合,其中有一个包含“myApp”子对象和“param”子对象的用户文档。

Currently I'm able to upsert 'param' in my user document except that what I want to do is to upsert to 'param' in 'myApp'.目前我可以在我的用户文档中更新“param”,但我想要做的是在“myApp”中更新“param”。

What is the good practice please?请问什么是好的做法?

Here is my current request:这是我目前的要求:

public ResponseEntity<?> setParam(String key, Param param){
        Query q = new Query(Criteria.where("_id").is(Auth.getUserId(key)));
        q.fields().include("myApp");
        Update u = Update.update("param", param).set("param", param);
        mongoTemplate.findAndModify(q, u, new FindAndModifyOptions().returnNew(true).upsert(false), User.class);
        return null;
}

Thank you in advance!先感谢您!

Ok I tried a lot of stuff and I finally found the solution!好吧,我尝试了很多东西,终于找到了解决方案!

It was enough in the .update to do "myApp.param" and in the .set to do "myApp.param" !在 .update 中执行 "myApp.param" 并在 .set 中执行 "myApp.param" 就足够了!

Hoping that it could be useful for others.希望它可以对其他人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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