java如何使用JPA在MongoDB中使用upsert进行更新
如果唯一的密钥组合不存在,我想在MongoDB中添加插入记录,否则在springboot中使用JPA插入记录
要插入的代码如下所示。在下面的示例中,id1和id2是唯一的密钥
@Autowired
MyEntityRepository myEntityRepository;
.........
MyEntity entity = new MyEntity();
entity.setId1(id1);
entity.setId2(id2);
entity.setTime(time);
myEntityRepository.save(entity);
如何使用upsert更新上述内容
# 1 楼答案
myEntityRepository。save()已使用upsert。如果密钥不存在,它将插入一条新记录
但我相信,如果是新记录,您希望保存在MongoDB中,而只有在它存在时才保存在DB中。在这种情况下,需要显式检查给定密钥的记录是否存在