public void updateIfMatch(final V value, final String eTag) { Bson query = and(eq(AbstractMongoRepository.ID, keyOf(value)), eq(ETAG, eTag)); Document updatedETaggable = collection().findOneAndReplace(query, encode(value), new FindOneAndReplaceOptions().returnDocument(AFTER)); if (isNull(updatedETaggable)) { Optional<V> findById = findOne(keyOf(value)); if (findById.isPresent()) { throw new ConcurrentModificationException("Entity concurrently modified: " + keyOf(value)); } throw new NotFoundException("Entity does not exist: " + keyOf(value)); } }