Custom method for update query with spring data MongoRepository(使用 Spring Data MongoRepository 进行更新查询的自定义方法)
问题描述
我正在使用 org.springframework.data.mongodb.repository.MongoRepository.我写了一些自定义方法,如下所示,
I am using org.springframework.data.mongodb.repository.MongoRepository. I have written some custom method like below,
public interface DocRepository extends MongoRepository<Doc, String> {
Doc findByDocIdAndAssignmentId(final String docId, final String assignemtId);
}
如何编写一个自定义方法,在满足条件时更新所有条目.
How can I write a custom method which update all entries when meeting a criteria.
例如,如果分配 id 为xyz",则将文档倾斜字段设置为abc"?
For example set document tilte field to "abc" if assignment id is "xyz"?
推荐答案
1) 您需要创建接口,例如 CustomDocRepository 并将此接口添加为您的 DocRepository 的 Base:
1) You need to create inteface e.g CustomDocRepository and add this interfaces as Base for your DocRepository:
public interface DocRepository extends MongoRepository<Doc, String>, CustomDocRepository {
void updateDocumentTitle(String id, String title);
}
2) 您需要为 DocRepository 添加实现:
2) You need to add implementation for the DocRepository:
@Repository
public class CustomDocRepositoryImpl implements DocRepository {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void updateDocumentTitle(String id, String title) {
Query query = new Query().addCriteria(where("_id").is(id));
Update update = new Update();
update.set("title", title);
mongoTemplate.update(Doc.class).matching(query).apply(update).first();
}
}
这就是你需要做的一切
这篇关于使用 Spring Data MongoRepository 进行更新查询的自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Spring Data MongoRepository 进行更新查询的自定义方法
基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
