本文整理汇总了Java中org.springframework.data.mongodb.core.query.Update类的典型用法代码示例。如果您正苦于以下问题:Java Update类的具体用法?Java Update怎么用?Java Update使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Update类属于org.springframework.data.mongodb.core.query包,在下文中一共展示了Update类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateUserChangeEmailField
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
/**
* update v5: {@link UserDocument} has changed; String-Field 'email' was replaced with custom Type 'Email'.
* find all user with an email String-value, replace String-Type with Custom-Type and update all UserDocuments.
* if email-Field is Empty or null, you don't have to update user.
*
* @since V6
*/
@ChangeSet(order = "006", id = "updateUserChangeEmailFromStringToCustomClass", author = "admin")
public void updateUserChangeEmailField(final MongoTemplate template) {
final Criteria isEmptyCriteria = new Criteria().orOperator(Criteria.where("email").is(""), Criteria.where("email").is(null));
while (true) {
final UserDocument result = template.findAndModify(new Query(isEmptyCriteria), Update.update("email", new Email()), UserDocument.class);
if (result == null)
break;
}
/**
* if email not null -> Field will be cast to specific class and updates will create correct entries
*/
// final Criteria isNotEmptyCriteria = new Criteria().orOperator(Criteria.where("email").ne(""), Criteria.where("email").ne(null));
// final List<UserDocument> userDocumentList = template.find(new Query(isNotEmptyCriteria), UserDocument.class);
}
示例2: updateRetry
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
/**
* 更改恢复次数
*
* @param id 事务id
* @param retry 恢复次数
* @param applicationName 应用名称
* @return true 成功
*/
@Override
public Boolean updateRetry(String id, Integer retry, 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;
}
示例3: assignRole
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
@PostMapping("/users/{userId}/roles/{role}")
void assignRole(@PathVariable String userId,
@PathVariable String role,
Authentication auth) {
if (can(auth, "ASSIGN", role)) {
final Role found = findRoleByName(role);
if (found == null) throw new RoleNotFoundException();
if (!mongo.updateFirst(
query(where("id").is(userId)),
new Update().addToSet("roles", found),
User.class
).isUpdateOfExisting()) {
throw new UserNotFoundException();
}
} else {
throw new OperationNotAllowedException();
}
}
示例4: revokeRole
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
@DeleteMapping("/users/{userId}/roles/{role}")
void revokeRole(@PathVariable String userId,
@PathVariable String role,
Authentication auth) {
if (can(auth, "REVOKE", role)) {
final Role found = findRoleByName(role);
if (found == null) throw new RoleNotFoundException();
if (!mongo.updateFirst(
query(where("id").is(userId)),
new Update().pull("roles", found),
User.class
).isUpdateOfExisting()) {
throw new UserNotFoundException();
}
} else {
throw new OperationNotAllowedException();
}
}
示例5: updateParticipant
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
/**
* 更新 List<Participant> 只更新这一个字段数据
*
* @param tccTransaction 实体对象
*/
@Override
public int updateParticipant(TccTransaction tccTransaction) {
Query query = new Query();
query.addCriteria(new Criteria("transId").is(tccTransaction.getTransId()));
Update update = new Update();
try {
update.set("contents", objectSerializer.serialize(tccTransaction.getParticipants()));
} catch (TccException e) {
e.printStackTrace();
}
final WriteResult writeResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
if (writeResult.getN() <= 0) {
throw new TccRuntimeException("更新数据异常!");
}
return 1;
}
示例6: updateFailTransaction
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
/**
* 更新事务失败日志
*
* @param mythTransaction 实体对象
* @return rows 1 成功
* @throws MythRuntimeException 异常信息
*/
@Override
public int updateFailTransaction(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("更新数据异常!");
}
return CommonConstant.SUCCESS;
}
示例7: updateParticipant
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
/**
* 更新 List<Participant> 只更新这一个字段数据
*
* @param mythTransaction 实体对象
*/
@Override
public int updateParticipant(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("更新数据异常!");
}
return CommonConstant.SUCCESS;
}
示例8: getNextSequenceId
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
public Long getNextSequenceId(String key) {
/** Get <code>Sequence</code> object by collection name*/
Query query = new Query(Criteria.where("id").is(key));
/** Increase field sequence by 1*/
Update update = new Update();
update.inc("sequence", 1);
// указываем опцию, что нужно возвращать измененный объект
/** Set option about returning new object*/
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
Sequence sequence = mongoOperations.findAndModify(query, update, options, Sequence.class);
// if no sequence set id value 'key'
if (sequence == null) {
sequence = setSequenceId(key, 1L);
}
return sequence.getCurrent();
}
示例9: saveResourcePlan
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
@Override
public boolean saveResourcePlan(ResourcePlan resourcePlan) {
boolean result = false;
if(resourcePlan.getAddTime() == 0) { //insert
resourcePlan.setAddTime(new Date().getTime());
resourcePlan.setModTime(new Date().getTime());
mongoTemplate.save(resourcePlan, Constant.COL_NAME_RESOURCE_PLAN);
result = Preconditions.isNotBlank(resourcePlan.getId());
} else { //update
Query query = new Query().addCriteria(Criteria.where("_id").is(resourcePlan.getId()));
Update update = new Update();
update.set("startPageNum", resourcePlan.getStartPageNum());
update.set("endPageNum", resourcePlan.getEndPageNum());
update.set("modTime", new Date().getTime());
WriteResult writeResult = mongoTemplate.updateFirst(query, update, Constant.COL_NAME_RESOURCE_PLAN);
result = writeResult!=null && writeResult.getN() > 0;
}
return result;
}
示例10: updateNotebook
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
public void updateNotebook(Notebook notebook){
Query query = new Query();
query.addCriteria(new Criteria("NotebookId").is(notebook.getNotebookId()));
Update update = new Update();
update.set("title", notebook.getTitle());
update.set("description", notebook.getDescription());
update.set("creator", notebook.getCreator());
update.set("owner", notebook.getOwner());
update.set("star", notebook.getStar());
update.set("collected", notebook.getCollected());
update.set("clickCount", notebook.getClickCount());
update.set("collaborators", notebook.getCollaborators());
update.set("contributors", notebook.getContributors());
update.set("notes", notebook.getNotes());
update.set("createTime", notebook.getCreateTime());
update.set("cover", notebook.getCover());
update.set("tags", notebook.getTags());
update.set("starers", notebook.getStarers());
mongoTemplate.updateFirst(query, update, Notebook.class,"Notebook");
}
示例11: updateUser
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
public void updateUser(User user){
Query query = new Query();
query.addCriteria(new Criteria("userId").is(user.getUserId()));
Update update = new Update();
update.set("username", user.getUsername());
update.set("personalStatus", user.getPersonalStatus());
update.set("notebooks", user.getNotebooks());
update.set("followers", user.getFollowers());
update.set("followings", user.getFollowings());
update.set("tags", user.getTags());
update.set("avatar", user.getAvatar());
update.set("collections", user.getCollections());
update.set("valid", user.getValid());
update.set("deleteCount", user.getDeleteTime());
update.set("reputation", user.getReputation());
update.set("qrcode", user.getQrcode());
mongoTemplate.updateFirst(query, update, User.class,"User");
}
示例12: createPasswordRecoveryToken
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
public SmartiUser createPasswordRecoveryToken(String login) {
final SmartiUser mongoUser = findUser(login);
if (mongoUser == null) {
return null;
}
final Date now = new Date(),
expiry = DateUtils.addHours(now, 24);
final String token = HashUtils.sha256(UUID.randomUUID() + mongoUser.getLogin());
final SmartiUser.PasswordRecovery recovery = new SmartiUser.PasswordRecovery(token, now, expiry);
final WriteResult result = updateMongoUser(mongoUser.getLogin(), Update.update(SmartiUser.FIELD_RECOVERY, recovery));
if (result.getN() == 1) {
return getSmaritUser(mongoUser.getLogin());
} else {
return null;
}
}
示例13: updateMessage
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
@Override
public Conversation updateMessage(ObjectId conversationId, Message message) {
final Query query = new Query(Criteria.where("_id").is(conversationId))
.addCriteria(Criteria.where("messages._id").is(message.getId()));
final Update update = new Update()
.set("messages.$", message)
.currentDate("lastModified");
final WriteResult writeResult = mongoTemplate.updateFirst(query, update, Conversation.class);
if (writeResult.getN() == 1) {
return mongoTemplate.findById(conversationId, Conversation.class);
} else {
return null;
}
}
示例14: saveIfNotLastModifiedAfter
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
@Override
public Conversation saveIfNotLastModifiedAfter(Conversation conversation, Date lastModified) {
final Query query = new Query();
query.addCriteria(Criteria.where("_id").is(conversation.getId()));
query.addCriteria(Criteria.where("lastModified").lte(lastModified));
BasicDBObject data = new BasicDBObject();
mongoTemplate.getConverter().write(conversation, data);
final Update update = new Update();
data.entrySet().stream()
.filter(e -> !Objects.equals("lastModified", e.getKey()))
.forEach(e -> update.set(e.getKey(), e.getValue()));
update.currentDate("lastModified");
final WriteResult writeResult = mongoTemplate.updateFirst(query, update, Conversation.class);
if (writeResult.getN() == 1) {
return mongoTemplate.findById(conversation.getId(), Conversation.class);
} else {
throw new ConcurrentModificationException(
String.format("Conversation %s has been modified after %tF_%<tT.%<tS (%tF_%<tT.%<tS)", conversation.getId(), lastModified, conversation.getLastModified()));
}
}
示例15: save
import org.springframework.data.mongodb.core.query.Update; //导入依赖的package包/类
public Document save(String id, Document document) {
Document _documentUpdate = findById(id);
if(null==_documentUpdate){
mongoTemplate.insert(document);
}else{
Query query = query(where("documentId").is(id));
Update update = new Update();
update.set("name",null == document.getName() ?
_documentUpdate.getName():document.getName());
update.set("location",null == document.getLocation() ?
_documentUpdate.getLocation():document.getLocation());
update.set("description",null == document.getDescription() ?
_documentUpdate.getDescription() : document.getDescription());
update.set("type",null == document.getType() ?
_documentUpdate.getType() : document.getType());
update.set("modified", new Date());
mongoTemplate.updateFirst(query, update, Document.class);
document = findById(id);
}
return document;
}