I'm currently trying to figure out how to update documents in MongoDb through Spring Data. Of course there is mongoTemplate.updateFirst and so on. But consider the following:
User u = mongoTemplate.findOne(new Query(Criteria.where("_id").is(s)), User.class);
if (u == null) throw new UsernameNotFoundException("user " + s + " does not exist");
Session.setCurrentUser(u);
mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(s)), new Update().inc("logincount", 1), User.class);
... to request the user, with a zero exception throw, if increment logincount is found to be 1. It works fine. But is that right? Do I need to request the user again? Can I change the object and re-save it?
source
share