@Nullable @Override public <T> T findAndModify(Query query, Update update, Class<T> entityClass, String collectionName) { return findAndModify(query, update, new FindAndModifyOptions(), entityClass, collectionName); }
@Nullable @Override public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass) { return findAndModify(query, update, options, entityClass, operations.determineCollectionName(entityClass)); }
protected T findAndModify(Query query, Update update, FindAndModifyOptions options) { return mongoTemplate.findAndModify(query, update, options, getDocumentClass()); }
@Override public @Nullable T findAndModifyValue() { return template.findAndModify(query, update, findAndModifyOptions != null ? findAndModifyOptions : new FindAndModifyOptions(), targetType, getCollectionName()); }
@Nullable @Override public <T> T findAndModify(Query query, Update update, Class<T> entityClass) { return findAndModify(query, update, new FindAndModifyOptions(), entityClass, operations.determineCollectionName(entityClass)); }
@Nullable @Override public <T> T findAndModify(Query query, Update update, Class<T> entityClass, String collectionName) { return findAndModify(query, update, new FindAndModifyOptions(), entityClass, collectionName); }
@Nullable @Override public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass) { return findAndModify(query, update, options, entityClass, operations.determineCollectionName(entityClass)); }
@Override public @Nullable T findAndModifyValue() { return template.findAndModify(query, update, findAndModifyOptions != null ? findAndModifyOptions : new FindAndModifyOptions(), targetType, getCollectionName()); }
@Nullable @Override public <T> T findAndModify(Query query, Update update, Class<T> entityClass) { return findAndModify(query, update, new FindAndModifyOptions(), entityClass, operations.determineCollectionName(entityClass)); }
/** * If the specified key is not already associated with a value, associate it with the given value. * This is equivalent to * <pre> {@code * if (!map.containsKey(key)) * return map.put(key, value); * else * return map.get(key); * }</pre> * except that the action is performed atomically. * <p> * @param key the metadata entry key * @param value the metadata entry value to store * @return null if successful, the old value otherwise. * @see java.util.concurrent.ConcurrentMap#putIfAbsent(Object, Object) */ @Override public String putIfAbsent(String key, String value) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(value, "'value' must not be empty."); Query query = new Query(Criteria.where(ID_FIELD).is(key)); query.fields().exclude(ID_FIELD); @SuppressWarnings("unchecked") Map<String, String> result = this.template.findAndModify(query, new Update().setOnInsert(VALUE, value), new FindAndModifyOptions().upsert(true), Map.class, this.collectionName); return result == null ? null : result.get(VALUE); }
private int getNextId() { Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME)); query.fields().include(SEQUENCE); return (Integer) this.template.findAndModify(query, new Update().inc(SEQUENCE, 1), FindAndModifyOptions.options().returnNew(true).upsert(true), Map.class, this.collectionName).get(SEQUENCE); // NOSONAR - never returns null }
/** * Perform MongoDB {@code INC} operation for the document, which contains the {@link MessageDocument} * {@code sequence}, and return the new incremented value for the new {@link MessageDocument}. * The {@link #SEQUENCE_NAME} document is created on demand. * @return the next sequence value. */ protected int getNextId() { Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME)); query.fields().include(MessageDocumentFields.SEQUENCE); return (Integer) this.mongoTemplate.findAndModify(query, new Update().inc(MessageDocumentFields.SEQUENCE, 1), FindAndModifyOptions.options().returnNew(true).upsert(true), Map.class, this.collectionName) .get(MessageDocumentFields.SEQUENCE); // NOSONAR - never returns null }
public T update(String id, Document updoc, T t) { Update update = Update.fromDocument(updoc, ""); Query query = Query.query(Criteria.where(Opations.ID_FIELD).is(id)); T result = (T) mongoTemplate.findAndModify(query, update, t.getClass()); return result; }
@Override public boolean set(String name, long value) { if(AssertUtils.isEmpty(name)) { throw new IllegalArgumentException("Counter name cannot be empty/null"); } Update update = new Update(); update.set("value", value); MongoCounter counter = this.mongoTemplate.findAndModify(new Query(Criteria.where("counterName").is(name)), update, MongoCounter.class); if(counter != null) { return true; } return false; }
@Override public void removeBinaryContent(String fileId) { Query query = query(where(BINARY_CONTENT_ID).is(fileId)); Update update = new Update(); update.unset(BINARY_CONTENT); mongoTemplate.findAndModify(query,update, Log.class); }
@Override public long increment(String name) { if(AssertUtils.isEmpty(name)) { throw new IllegalArgumentException("Counter name cannot be empty/null"); } Update update = new Update(); update.inc("value", 1); MongoCounter counter = this.mongoTemplate.findAndModify(new Query(Criteria.where("counterName").is(name)), update, MongoCounter.class); if(counter != null) { return counter.getValue(); } return 0l; }
@Override public long next(String name) { SequenceEntity seq = mongoTemplate.findAndModify( Query.query(Criteria.where("_id").is(name)), new Update().inc("seq", 1L), SequenceEntity.class); return (seq != null ? seq.getSeq() : 0L); }
private int getNextId() { Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME)); query.fields().include(SEQUENCE); return (Integer) this.template.findAndModify(query, new Update().inc(SEQUENCE, 1), FindAndModifyOptions.options().returnNew(true).upsert(true), Map.class, this.collectionName).get(SEQUENCE); // NOSONAR - never returns null }
private long increaseCounter(String counterName){ Query query = new Query(Criteria.where("name").is(counterName)); Update update = new Update().inc("sequence", 1); Counter counter = mongoTemplate.findAndModify(query, update, Counter.class); // return old Counter object if (counter == null){ counter = new Counter(); counter.setName(counterName); counter.setSequence(2); //should increase by one. mongoTemplate.save(counter); return 1; } return counter.getSequence(); }
@Override public void incrementDailyStatistic(String day, String serviceName, String serviceVersion, String hourKey, String minuteKey){ // Build a query to select specific object within collection. Query query = new Query(Criteria.where("day").is(day) .and("serviceName").is(serviceName) .and("serviceVersion").is(serviceVersion)); // Other way to build a query using statically imported methods. //Query queryShort = query(where("day").is(day).and("serviceName").is(serviceName).and("serviceVersion").is(serviceVersion)); // Build update to increment the 3 fields. Update update = new Update().inc("dailyCount", 1).inc("hourlyCount." + hourKey, 1).inc("minuteCount." + minuteKey, 1); // Do an upsert with find and modify. template.findAndModify(query, update, DailyStatistic.class); }