我尝试在Mongodb的Spring Boot中使用简单的CRUD。我的身份证号码有问题。如何自动增加ID。 I tried but couldn't do it
有没有简单的自动递增方式?
控制器
@Autowired
EmployeeRepo repo;
@RequestMapping(value = "home", method = RequestMethod.GET)
public String getHomePage(Model model) {
Employee employee = new Employee();
employee.setId(1);
employee.setName("deniz");
employee.setPassword("123");
repo.save(employee);
...
员工
@Document(collection = "Employee")
public class Employee {
@Id
private long id;
private String name;
private String password;
// getter and setter
答案 0 :(得分:0)
public long getNextSequenceId(String key) {
Query query = new Query(Criteria.where("_id").is(key));
Update update = new Update();
update.inc("seq", 1);
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
SequenceId seqId =
mongoOperation.findAndModify(query, update, options, SequenceId.class);
return seqId.getSeq();
}