我在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分钟的那些列。如何达到此要求?