简体   繁体   English

spring数据和mongodb:更新现有文档的问题

[英]spring data and mongodb : issue in updating existing document

creating a document with status as pending and generated-token then send mail创建状态为待处理和生成令牌的文档,然后发送邮件

and immediately after sending mail retrieving document by generated-token value and changing the previous status pending to unverified.并在发送邮件后立即通过生成的令牌值检索文档并将之前的状态更改为未验证。

Even though for updating the document status, first retrieved the existing document and then only updating it, still end up in creating two different documents for both the status.即使为了更新文档状态,首先检索现有文档然后才更新它,最终仍会为这两种状态创建两个不同的文档。

@Document
public class VerificationInfo {         
    private LoginInfo user; 
    private String token; 
    private String verificationStatus = VerificationStatus.PENDING.getVerificationStatus();
}

daoService道服务

public void updateStatus(VerificationInfo verificationToken, String status) {
        VerificationInfo vt = verificationRepository.findByToken(verificationToken.getToken()).get(0);
        vt.setVerificationStatus(status);
        verificationRepository.save(vt);
    }

repository存储库

@Repository
public interface VerificationRepository extends MongoRepository<VerificationInfo, String> {
     List<VerificationInfo> findByToken(String token);
     List<VerificationInfo> findByUser(LoginInfo user);
}

db entries数据库条目

{ "_id" : ObjectId("5f4e7486664e197f3d745b17"), "token" : "c82907b7-e13e-484d-89cf-92ea394b6f6d", "verificationStatus" : "pending", "_class" : "com.models.VerificationInfo" }
{ "_id" : ObjectId("5f4e748b664e197f3d745b18"), "token" : "c82907b7-e13e-484d-89cf-92ea394b6f6d", "verificationStatus" : "unverified", "_class" : "com.models.VerificationInfo" }

If the status is correct, the problem with you identification of the document (_id) .如果状态正确,说明你识别的文件(_id)有问题

public class VerificationInfo {
    @Id
    ObjectId _id;
    // Other fields
}

Here we set a unique id to each document.这里我们为每个文档设置了一个唯一的 id。 So when you create a object, it will create a new document.所以当你创建一个对象时,它会创建一个新的文档。 If the _id already exists in database, then it will update the document based on the particular id.如果 _id 已经存在于数据库中,那么它将根据特定的 id 更新文档。

1. There is no _id present in the model class 1.模型类中没有_id

You extends MongoRepository<VerificationInfo, String> , the second parameter is the type of id .extends MongoRepository<VerificationInfo, String> ,第二个参数是id的类型。 But there is no any id found in your model class.但是在您的模型类中没有找到任何id (Usually we use ObjectId , but String also can be given) (通常我们使用ObjectId ,但也可以给出String

2. It will always create new document when you get data from frontend 2.当你从前端获取数据时,它总是会创建新文档

Since you don't have id, when you pass a data to updateStatus(VerificationInfo verificationToken, String status) , it will create new id and set the data, that's why you always getting new document.由于您没有 id,因此当您将数据传递给updateStatus(VerificationInfo verificationToken, String status) ,它将创建新的id并设置数据,这就是您总是获得新文档的原因。

Suppose you are sending the data with existing id , then the existing document will be updated based on given id假设您使用现有id发送数据,那么现有文档将根据给定的id进行更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM