org.springframework.data.mongodb.core.MongoTemplate.findAndModify()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(127)

本文整理了Java中org.springframework.data.mongodb.core.MongoTemplate.findAndModify()方法的一些代码示例,展示了MongoTemplate.findAndModify()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MongoTemplate.findAndModify()方法的具体详情如下:
包路径:org.springframework.data.mongodb.core.MongoTemplate
类名称:MongoTemplate
方法名:findAndModify

MongoTemplate.findAndModify介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-data-mongodb

@Nullable
@Override
public <T> T findAndModify(Query query, Update update, Class<T> entityClass, String collectionName) {
  return findAndModify(query, update, new FindAndModifyOptions(), entityClass, collectionName);
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Nullable
@Override
public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass) {
  return findAndModify(query, update, options, entityClass, operations.determineCollectionName(entityClass));
}

代码示例来源:origin: kaaproject/kaa

protected T findAndModify(Query query, Update update, FindAndModifyOptions options) {
 return mongoTemplate.findAndModify(query, update, options, getDocumentClass());
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public @Nullable T findAndModifyValue() {
  return template.findAndModify(query, update,
      findAndModifyOptions != null ? findAndModifyOptions : new FindAndModifyOptions(), targetType,
      getCollectionName());
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Nullable
@Override
public <T> T findAndModify(Query query, Update update, Class<T> entityClass) {
  return findAndModify(query, update, new FindAndModifyOptions(), entityClass,
      operations.determineCollectionName(entityClass));
}

代码示例来源:origin: org.springframework.data/spring-data-mongodb

@Nullable
@Override
public <T> T findAndModify(Query query, Update update, Class<T> entityClass, String collectionName) {
  return findAndModify(query, update, new FindAndModifyOptions(), entityClass, collectionName);
}

代码示例来源:origin: org.springframework.data/spring-data-mongodb

@Nullable
@Override
public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass) {
  return findAndModify(query, update, options, entityClass, operations.determineCollectionName(entityClass));
}

代码示例来源:origin: org.springframework.data/spring-data-mongodb

@Override
public @Nullable T findAndModifyValue() {
  return template.findAndModify(query, update,
      findAndModifyOptions != null ? findAndModifyOptions : new FindAndModifyOptions(), targetType,
      getCollectionName());
}

代码示例来源:origin: org.springframework.data/spring-data-mongodb

@Nullable
@Override
public <T> T findAndModify(Query query, Update update, Class<T> entityClass) {
  return findAndModify(query, update, new FindAndModifyOptions(), entityClass,
      operations.determineCollectionName(entityClass));
}

代码示例来源:origin: spring-projects/spring-integration

/**
 * 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);
}

代码示例来源:origin: spring-projects/spring-integration

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
}

代码示例来源:origin: spring-projects/spring-integration

/**
 * 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
}

代码示例来源:origin: onsoul/HA-DB

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;
}

代码示例来源:origin: com.sangupta/jerry-services

@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;
}

代码示例来源:origin: com.epam.reportportal/commons-dao

@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);
}

代码示例来源:origin: com.sangupta/jerry-services

@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;
}

代码示例来源:origin: com.googlecode.web-commons/web-common-dao

@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);
}

代码示例来源:origin: org.springframework.integration/spring-integration-mongodb

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
}

代码示例来源:origin: jiwhiz/JiwhizBlogWeb

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();
}

代码示例来源:origin: microcks/microcks

@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);
}

相关文章