重复键错误Spring mongo存储库保存方法

时间:2019-10-15 02:02:55

标签: spring mongodb

我收到此错误E11000重复密钥错误集合:authentication.userdetails索引:用户dup密钥:

型号

@Document(collection = "userdetails")
public class UserProfileDetails {
    @Id
    private ObjectId id;
    @Indexed(unique = true)
    private User user;
    private String firstName;
    private String middleName;
    private String lastName;
    private Boolean isActive;

    private String address;
    private String city;
    private String state;
    private String zipCode;

当我编辑用户个人资料并尝试保存更改时,在尝试保存新的个人资料数据时,我一直收到重复的键错误。

    @Override
    public UserProfileDetails save(UserProfileDetails userProfileDetails) {
        UserProfileDetails loggedInUserHasProfile = userProfileRepository.findUserFromProfile(userProfileDetails.getUser());
        if(loggedInUserHasProfile != null){
            return userProfileRepository.save(userProfileDetails);
        } else {
            return userProfileRepository.save(userProfileDetails);
        }
    }

0 个答案:

没有答案