如果当前时间-createdTime大于10分钟,如何使用Spring Boot更新MongoDB中的列?

时间:2018-12-05 13:33:36

标签: java mongodb spring-boot mongodb-query mongotemplate

我在MongoDB中有一个这样的集合

{
    "_id": "5c0792ce76bf172e7b4f554e",
    "_class": "com.altaf.health.entity.OTP",
    "createdTime": "2018-12-05T08:56:46.199Z",
    "updatedTime": "2018-12-05T08:56:46.199Z",
    "encryptedOTP": "ECH2IsEvQyM=",
    "isExpired": false,
    "mobile": "1234567890"
}

我的模型课是

@Document(collection="otp")
public class OTP {
    @Id
    private String otpId;
    private Date createdTime;
    private Date updatedTime;
    private String encryptedOTP;
    private boolean isExpired;
    private String mobile;
    ....
}

现在,如果isExpired-true大于10分钟,我想用值current time更新列createdTime

  

为此,我已经编写了这段代码

import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
....

Query query = new Query();
query.addCriteria(Criteria.where("isExpired").is(false));
Update update=new Update();
update.set("isExpired", true);
WriteResult writeResult = mongoTemplate.updateMulti(query, update, OTP.class);
System.out.println("write object = "+writeResult);
  

但是此代码将更新isExpired值为false的所有记录。在这里,我只想更新current time-createdTime大于10分钟的那些列。如何达到此要求?

0 个答案:

没有答案