繁体   English   中英

如何使用MongoTemplate向MongoDB查询“ Timestamp()或Date()”?

[英]How do I query “Timestamp() or Date()” with MongoTemplate to MongoDB?

我使用MongoTemplate处理MongoDB

我想将文档的列更新为当前时间

在Mongodb命令行客户端中,它将与

db.collectionName.update({_ id:1},{timeCol:new Timestamp()}); 或db.collectionName.update({_ id:1},{timeCol:new Date()});或

但是我不知道如何使用mongoTemplate做到这一点。

更新更新; update.set(“ timeCol”,“ new Timestamp()”); //当然不起作用

请帮我

将当前时间戳构建为

Date currentDate = new Date();
Timestamp timeStamp = new Timestamp(currentDate.getTime());

然后像这样更新集合:

Query query = new Query();
query.addCriteria(Criteria.where("_id").is(1));

Update update = new Update();
update.set("timeCol", timeStamp);

mongoOperations.updateFirst(query, update, "collectionName");

从Spring-data-mongodb 1.6版本更新这样的Collection,它将使用MongoDb当前日期

Query query = new Query();
query.addCriteria(Criteria.where("_id").is(1));

Update update = new Update();
update.currentDate("timeCol")

mongoOperations.updateFirst(query, update, "collectionName");

如果要使用时间戳,请使用update.currentTimestamp(key);。 而不是update.currentDate(key)

暂无
暂无

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

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