我已经使用MongoTemplate的findAndModify方法,如下所示:
public Organization findAndModifyOrganization(Integer orgId, String name, Integer empId)
{
Query query = new Query();
query.addCriteria(Criteria.where("employees.id").is(empId));
query.addCriteria(Criteria.where("id").is(orgId));
Update update = new Update();
update.set("employees.$.name", name);
Organization org = mongoTemplate.findAndModify(query, update, new FindAndModifyOptions().returnNew(true), Organization.class);
return org;
}
//类结构
@Document
public class Organization {
private Integer id;
private List<Employee> employees;
}
@Document
public class Employee {
private Integer id;
private String name;
}
我希望使用Spring Data MongoRepository方法来完成上述操作。请告诉我如何使用MongoRepository方法更新员工的名称。