[英]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.