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

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

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

MongoTemplate.updateFirst介绍

暂无

代码示例

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

protected void updateFirst(Query query, Update update) {
 mongoTemplate.updateFirst(query, update, getDocumentClass());
}

代码示例来源:origin: yu199195/myth

@Override
public Boolean updateRetry(final String id, final Integer retry, final String appName) {
  if (StringUtils.isBlank(id) || StringUtils.isBlank(appName) || Objects.isNull(retry)) {
    return Boolean.FALSE;
  }
  final String mongoTableName = RepositoryPathUtils.buildMongoTableName(appName);
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  Update update = new Update();
  update.set("lastTime", DateUtils.getCurrentDateTime());
  update.set("retriedCount", retry);
  final WriteResult writeResult = mongoTemplate.updateFirst(query, update,
      MongoAdapter.class, mongoTableName);
  if (writeResult.getN() <= 0) {
    throw new RuntimeException("更新数据异常!");
  }
  return Boolean.TRUE;
}

代码示例来源:origin: yu199195/hmily

@Override
public Boolean updateRetry(final String id, final Integer retry, final String appName) {
  if (StringUtils.isBlank(id) || StringUtils.isBlank(appName) || Objects.isNull(retry)) {
    return Boolean.FALSE;
  }
  final String mongoTableName = RepositoryPathUtils.buildMongoTableName(appName);
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  Update update = new Update();
  update.set("lastTime", DateUtils.getCurrentDateTime());
  update.set("retriedCount", retry);
  final UpdateResult updateResult = mongoTemplate.updateFirst(query, update,
      MongoAdapter.class, mongoTableName);
  if (updateResult.getModifiedCount() <= 0) {
    throw new HmilyRuntimeException("更新数据异常!");
  }
  return Boolean.TRUE;
}

代码示例来源:origin: yu199195/Raincat

@Override
public Boolean updateRetry(final String id, final Integer retry, final String applicationName) {
  if (StringUtils.isBlank(id)
      || StringUtils.isBlank(applicationName)
      || Objects.isNull(retry)) {
    return Boolean.FALSE;
  }
  final String mongoTableName = RepositoryPathUtils.buildMongoTableName(applicationName);
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  Update update = new Update();
  update.set("lastTime", DateUtils.getCurrentDateTime());
  update.set("retriedCount", retry);
  final WriteResult writeResult = mongoTemplate.updateFirst(query, update,
      MongoAdapter.class, mongoTableName);
  if (writeResult.getN() <= 0) {
    throw new TransactionRuntimeException("更新数据异常!");
  }
  return Boolean.TRUE;
}

代码示例来源:origin: yu199195/hmily

@Override
public int updateStatus(final String id, final Integer status) {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  Update update = new Update();
  update.set("status", status);
  final UpdateResult updateResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (updateResult.getModifiedCount() <= 0) {
    throw new HmilyRuntimeException("update data exception!");
  }
  return ROWS;
}

代码示例来源:origin: yu199195/myth

@Override
public int updateStatus(final String id, final Integer status) throws MythRuntimeException {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  Update update = new Update();
  update.set("status", status);
  final WriteResult writeResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (writeResult.getN() <= 0) {
    throw new MythRuntimeException(ERROR);
  }
  return CommonConstant.SUCCESS;
}

代码示例来源:origin: ityouknow/spring-boot-examples

/**
 * 更新对象
 * @param user
 */
@Override
public int updateUser(UserEntity user) {
  Query query=new Query(Criteria.where("id").is(user.getId()));
  Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
  //更新查询返回结果集的第一条
  WriteResult result =mongoTemplate.updateFirst(query,update,UserEntity.class);
  //更新查询返回结果集的所有
  // mongoTemplate.updateMulti(query,update,UserEntity.class);
  if(result!=null)
    return result.getN();
  else
    return 0;
}

代码示例来源:origin: yu199195/myth

@Override
public void updateFailTransaction(final MythTransaction mythTransaction) throws MythRuntimeException {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(mythTransaction.getTransId()));
  Update update = new Update();
  update.set("status", mythTransaction.getStatus());
  update.set("errorMsg", mythTransaction.getErrorMsg());
  update.set("lastTime", new Date());
  update.set("retriedCount", mythTransaction.getRetriedCount());
  final WriteResult writeResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (writeResult.getN() <= 0) {
    throw new MythRuntimeException(ERROR);
  }
}

代码示例来源:origin: yu199195/hmily

@Override
public int update(final HmilyTransaction hmilyTransaction) throws HmilyRuntimeException {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(hmilyTransaction.getTransId()));
  Update update = new Update();
  update.set("lastTime", new Date());
  update.set("retriedCount", hmilyTransaction.getRetriedCount() + 1);
  update.set("version", hmilyTransaction.getVersion() + 1);
  try {
    if (CollectionUtils.isNotEmpty(hmilyTransaction.getHmilyParticipants())) {
      update.set("contents", objectSerializer.serialize(hmilyTransaction.getHmilyParticipants()));
    }
  } catch (HmilyException e) {
    e.printStackTrace();
  }
  final UpdateResult updateResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (updateResult.getModifiedCount() <= 0) {
    throw new HmilyRuntimeException("update data exception!");
  }
  return ROWS;
}

代码示例来源:origin: yu199195/hmily

@Override
public int updateParticipant(final HmilyTransaction hmilyTransaction) {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(hmilyTransaction.getTransId()));
  Update update = new Update();
  try {
    update.set("contents", objectSerializer.serialize(hmilyTransaction.getHmilyParticipants()));
  } catch (HmilyException e) {
    e.printStackTrace();
  }
  final UpdateResult updateResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (updateResult.getModifiedCount() <= 0) {
    throw new HmilyRuntimeException("update data exception!");
  }
  return ROWS;
}

代码示例来源:origin: yu199195/myth

@Override
public int update(final MythTransaction mythTransaction) throws MythRuntimeException {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(mythTransaction.getTransId()));
  Update update = new Update();
  update.set("lastTime", new Date());
  update.set("retriedCount", mythTransaction.getRetriedCount() + 1);
  update.set("version", mythTransaction.getVersion() + 1);
  try {
    if (CollectionUtils.isNotEmpty(mythTransaction.getMythParticipants())) {
      update.set("contents", objectSerializer.serialize(mythTransaction.getMythParticipants()));
    }
  } catch (MythException e) {
    e.printStackTrace();
  }
  final WriteResult writeResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (writeResult.getN() <= 0) {
    throw new MythRuntimeException(ERROR);
  }
  return CommonConstant.SUCCESS;
}

代码示例来源:origin: yu199195/myth

@Override
public void updateParticipant(final MythTransaction mythTransaction) throws MythRuntimeException {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(mythTransaction.getTransId()));
  Update update = new Update();
  try {
    update.set("contents", objectSerializer.serialize(mythTransaction.getMythParticipants()));
  } catch (MythException e) {
    e.printStackTrace();
  }
  final WriteResult writeResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (writeResult.getN() <= 0) {
    throw new MythRuntimeException(ERROR);
  }
}

代码示例来源:origin: yu199195/Raincat

@Override
public int update(final TransactionRecover transactionRecover) throws TransactionRuntimeException {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(transactionRecover.getId()));
  Update update = new Update();
  if (CompensationOperationTypeEnum.TASK_EXECUTE.getCode() == transactionRecover.getOperation()) {
    update.set("completeFlag",CommonConstant.TX_TRANSACTION_COMPLETE_FLAG_OK);
  } else if (CompensationOperationTypeEnum.COMPENSATION.getCode() == transactionRecover.getOperation()) {
    update.set("lastTime", new Date());
    update.set("retriedCount", transactionRecover.getRetriedCount() + 1);
    update.set("version", transactionRecover.getVersion() + 1);
  }
  final WriteResult writeResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (writeResult.getN() <= 0) {
    throw new TransactionRuntimeException(UPDATE_EX);
  }
  return ROWS;
}

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

private void updateGroup(Object groupId, Update update) {
  this.mongoTemplate.updateFirst(groupOrderQuery(groupId), update, this.collectionName);
}

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

private void updateGroup(Object groupId, Update update) {
  Query query = whereGroupIdIs(groupId).with(new Sort(Sort.Direction.DESC, GROUP_UPDATE_TIMESTAMP_KEY, SEQUENCE));
  this.template.updateFirst(query, update, this.collectionName);
}

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

/**
 * Replace an existing metadata entry {@code value} with a new one. Otherwise does nothing.
 * Performs {@code updateFirst} if a document for the provided {@code key} and {@code oldValue}
 * exists in the {@link #collectionName}.
 * @param key the metadata entry key
 * @param oldValue the metadata entry old value to replace
 * @param newValue the metadata entry new value to put
 * @return {@code true} if replace was successful, {@code false} otherwise.
 * @see MongoTemplate#updateFirst(Query, Update, String)
 */
@Override
public boolean replace(String key, String oldValue, String newValue) {
  Assert.hasText(key, "'key' must not be empty.");
  Assert.hasText(oldValue, "'oldValue' must not be empty.");
  Assert.hasText(newValue, "'newValue' must not be empty.");
  Query query = new Query(Criteria.where(ID_FIELD).is(key).and(VALUE).is(oldValue));
  return this.template.updateFirst(query, Update.update(VALUE, newValue), this.collectionName)
      .getModifiedCount() > 0;
}

代码示例来源:origin: flyleft/xmarket-server

@Override
public void updateUserPhoneSchool(String userId,String phone, String school){
  template.updateFirst(new Query(where("_id").is(userId)),
      new Update().set("school",school), User.class);
  template.updateFirst(new Query(where("_id").is(userId)),
      new Update().set("phone",phone), User.class);
}

代码示例来源:origin: flyleft/xmarket-server

@Override
public void addTradeTag(TradeTag tradeTag) {
  template.updateFirst(new Query(where("name").is(SysColName.colTradeTag.name())),
      new Update().pull("tradeTags",tradeTag), SystemBean.class);
}

代码示例来源:origin: rstyro/Springboot

/**
 * 更新 通过id
 */
@Override
public int upadteUserById(User user) {
  Query query = new Query(Criteria.where("id").is(user.getId()));
  Update update = new Update();
  update.set("name", user.getName()).set("age", user.getAge());
  WriteResult result =  mongoTemplate.updateFirst(query, update, User.class);
  return result.getN();
}

代码示例来源:origin: souyunku/SpringBootExamples

@Override
public void updateDemo(DemoEntity demoEntity) {
  Query query = new Query(Criteria.where("id").is(demoEntity.getId()));
  Update update = new Update();
  update.set("title", demoEntity.getTitle());
  update.set("description", demoEntity.getDescription());
  update.set("by", demoEntity.getBy());
  update.set("url", demoEntity.getUrl());
  mongoTemplate.updateFirst(query, update, DemoEntity.class);
}

相关文章