春季数据与MongoDB:更新现有文档中的问题

huangapple go评论74阅读模式
英文:

spring data and mongodb : issue in updating existing document

问题

创建一个带有状态为待处理(pending)和生成的令牌(generated-token)的文档,然后发送邮件。

在发送邮件后立即通过生成的令牌值检索文档,并将先前的状态从待处理(pending)更改为未验证(unverified)。

即使要更新文档状态,首先检索现有文档,然后才更新它,最终还是会为两种状态分别创建两个不同的文档。

@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);
}

数据库条目

{ "_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" }
英文:

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&lt;VerificationInfo, String&gt; {
	 List&lt;VerificationInfo&gt; findByToken(String token);
	 List&lt;VerificationInfo&gt; findByUser(LoginInfo user);
}

db entries

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

答案1

得分: 1

如果状态正确,则是关于您对文档(_id)的识别的问题。

public class VerificationInfo {
    @Id
    ObjectId _id;
    // 其他字段
}

在这里,我们为每个文档设置了一个唯一的 id。因此,当您创建一个对象时,它将创建一个新的文档。如果数据库中已经存在 _id,则会根据特定的 id 更新文档。

1. 模型类中没有 _id 字段

您的类扩展了 MongoRepository&lt;VerificationInfo, String&gt;,第二个参数是 id 的类型。但是在您的模型类中没有找到任何 id 字段。(通常我们使用 ObjectId,但也可以使用 String

2. 从前端获取数据时将始终创建新的文档

由于您没有 id,当您将数据传递给 updateStatus(VerificationInfo verificationToken, String status) 时,它将创建新的 id 并设置数据,这就是您始终获得新文档的原因。

假设您发送具有现有 id 的数据,则将根据给定的 id 更新现有文档。

英文:

If the status is correct, the problem with you identification of the document (_id).

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

Here we set a unique id to each document. 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.

1. There is no _id present in the model class

You extends MongoRepository&lt;VerificationInfo, String&gt;, the second parameter is the type of id. But there is no any id found in your model class. (Usually we use ObjectId, but String also can be given)

2. It will always create new document when you get data from frontend

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.

Suppose you are sending the data with existing id, then the existing document will be updated based on given id

huangapple
  • 本文由 发表于 2020年9月2日 01:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63692507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定