[英]How do I query “Timestamp() or Date()” with MongoTemplate to MongoDB?
I use MongoTemplate to handle MongoDB 我使用MongoTemplate处理MongoDB
I want update documents' column to current time 我想将文档的列更新为当前时间
In Mongodb command-line client, it will work with 在Mongodb命令行客户端中,它将与
db.collectionName.update({_id:1}, {timeCol: new Timestamp()}); db.collectionName.update({_ id:1},{timeCol:new Timestamp()}); or db.collectionName.update({_id:1}, {timeCol: new Date()}); 或db.collectionName.update({_ id:1},{timeCol:new Date()});或
But I don't know how I do that by using mongoTemplate. 但是我不知道如何使用mongoTemplate做到这一点。
Update update; 更新更新; update.set("timeCol", "new Timestamp()"); update.set(“ timeCol”,“ new Timestamp()”); // of course, it doesn't work //当然不起作用
Plz help me 请帮我
Build current timestamp as 将当前时间戳构建为
Date currentDate = new Date();
Timestamp timeStamp = new Timestamp(currentDate.getTime());
Then update collection like this : 然后像这样更新集合:
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(1));
Update update = new Update();
update.set("timeCol", timeStamp);
mongoOperations.updateFirst(query, update, "collectionName");
Update Collection like this from Spring-data-mongodb 1.6 version, this will use MongoDb current Date 从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");
If you want Timestamp use update.currentTimestamp(key); 如果要使用时间戳,请使用update.currentTimestamp(key);。 instead of update.currentDate(key) 而不是update.currentDate(key)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.