long start = start();
try {
// perform a conditional update with limited result
// if we have a matching modCount
if (modCount != null) {
QueryBuilder query = createQueryForUpdate(updateOp, checkConditions);
query.and(Document.MOD_COUNT).is(modCount);
DBObject fields = new BasicDBObject();
// return _id only
fields.put("_id", 1);
DBObject oldNode = dbCollection.findAndModify(query.get(), fields,
null /*sort*/, false /*remove*/, update, false /*returnNew*/,
false /*upsert*/);
if (oldNode != null) {
// success, update cached document
applyToCache(collection, cachedDoc, updateOp);
// return previously cached document
return cachedDoc;
}
}
// conditional update failed or not possible
// perform operation and get complete document
QueryBuilder query = createQueryForUpdate(updateOp, checkConditions);
DBObject oldNode = dbCollection.findAndModify(query.get(), null,
null /*sort*/, false /*remove*/, update, false /*returnNew*/,
upsert);
if (checkConditions && oldNode == null) {
return null;
}