1. In order to ensure data consistency and prevent atomicity at high concurrency, findAndModify can be used to achieve
Update update = new Update().inc("age", 1); mongoTemplate.findAndModify(query, update, Person.class); // return's old person object template.findAndModify(query, update, new FindAndModifyOptions().returnNew(true), Person.class);// Now return the newly updated document when updating mongoTemplate.findAndModify(query2, update, new FindAndModifyOptions().returnNew(true).upsert(true), Person.class);//Insert if not
Reference: http://runfriends.iteye.com/blog/1830749
Reference: http://www.tuicool.com/articles/QZVNbe
MongoDB uses the lock
1. MongoDB uses the "readers-writer" lock, which is read and write Lock.
2. Lock granularity. Before version 2.2, mongod only had global locks; starting from version 2.2, most read and write operations only locked one library. Compared with previous versions, this granularity has been reduced.
Reference: http://www.bubuko.com/infodetail-998672.html