我正在使用 Spring data MongoTemplate 来管理 mongo 操作。我正在尝试保存和更新 json 完整文档(在 java 中使用 String.class)。
例子:
String content = "{MyId": "1","code":"UG","variables":[1,2,3,4,5]}";
String updatedContent = "{MyId": "1","code":"XX","variables":[6,7,8,9,10]}";
我知道我可以使用以下方法独立更新代码和变量:
Query query = new Query(where("MyId").is("1"));
Update update1 = new Update().set("code", "XX");
getMongoTemplate().upsert(query, update1, collectionId);
Update update2 = new Update().set("variables", "[6,7,8,9,10]");
getMongoTemplate().upsert(query, update2, collectionId);
但是由于我们的应用程序架构,直接替换整个对象可能对我们更有用。我所知:
getMongoTemplate().save(content,collectionId)
getMongoTemplate().save(updatedContent,collectionId)
实现 saveOrUpdate 功能,但这会创建两个对象,不更新任何东西。
我错过了什么?有什么办法吗?谢谢