如何在春季使用 MongoRepository 接口更新 mongo db 集合中的特定字段?
问问题
19825 次
2 回答
5
您可以通过以下代码更新特定字段:
Query query1 = new Query(Criteria.where("id").is("123"));
Update update1 = new Update();
update1.set("available", false);
mongoTemplate.updateFirst(query1, update1, Customer.class);
于 2017-07-07T19:13:29.503 回答
3
截至今天,您无法MongoRepository
使用一个查询来更新文档。MongoRepository
如果您真的想使用以下步骤更新特定字段:
- 获取要更新的文档
- 将新值设置为特定字段并保存该文档。
例子:
MyDocument myDocumentToUpdate = myDocumentRepository.findById(documentId); // fetching a document that you want to update the field
myDocumentToUpdate.setMyField(myNewValue); // setting the new value to the field myField
myDocumentRepository.save(myDocumentToUpdate); // saving (It basically updates the document) the updated document
于 2020-06-26T11:02:11.180 回答