Reputation: 161
I use MongoTemplate to handle MongoDB
I want update documents' column to current time
In Mongodb command-line client, it will work with
db.collectionName.update({_id:1}, {timeCol: new Timestamp()}); or db.collectionName.update({_id:1}, {timeCol: new Date()});
But I don't know how I do that by using mongoTemplate.
Update update; update.set("timeCol", "new Timestamp()"); // of course, it doesn't work
Plz help me
Upvotes: 2
Views: 2049
Reputation: 3601
Update Collection like this from Spring-data-mongodb 1.6 version, this will use MongoDb current Date
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); instead of update.currentDate(key)
Upvotes: 1
Reputation: 1345
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");
Upvotes: 0