本文整理汇总了Java中com.mongodb.WriteResult类的典型用法代码示例。如果您正苦于以下问题:Java WriteResult类的具体用法?Java WriteResult怎么用?Java WriteResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WriteResult类属于com.mongodb包,在下文中一共展示了WriteResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateRetry
import com.mongodb.WriteResult; //导入依赖的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;
}
示例2: updateParticipant
import com.mongodb.WriteResult; //导入依赖的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;
}
示例3: updateFailTransaction
import com.mongodb.WriteResult; //导入依赖的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;
}
示例4: updateParticipant
import com.mongodb.WriteResult; //导入依赖的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;
}
示例5: saveResourcePlan
import com.mongodb.WriteResult; //导入依赖的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;
}
示例6: createPasswordRecoveryToken
import com.mongodb.WriteResult; //导入依赖的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;
}
}
示例7: updateMessage
import com.mongodb.WriteResult; //导入依赖的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;
}
}
示例8: saveIfNotLastModifiedAfter
import com.mongodb.WriteResult; //导入依赖的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()));
}
}
示例9: shouldInsertOneHeroWithAutomaticObjectId
import com.mongodb.WriteResult; //导入依赖的package包/类
@Test
public void shouldInsertOneHeroWithAutomaticObjectId() {
//GIVEN
Address castleWinterfell = new Address("Winterfell", "Westeros", Region.THE_NORTH);
Set<Human> children = Sets.newHashSet();
children.add(Hero.createHeroWithoutChildrenAndNoBeasts("Robb", "Stark", castleWinterfell));
children.add(Heroine.createHeroineWithoutChildrenAndNoBeasts("Sansa", "Stark", castleWinterfell));
children.add(Heroine.createHeroineWithoutChildrenAndNoBeasts("Arya", "Stark", castleWinterfell));
children.add(Hero.createHeroWithoutChildrenAndNoBeasts("Bran", "Stark", castleWinterfell));
children.add(Hero.createHeroWithoutChildrenAndNoBeasts("Rickon", "Stark", castleWinterfell));
children.add(Hero.createHeroWithoutChildrenAndNoBeasts("Jon", "Snow", castleWinterfell));
Hero eddardStark = Hero.createHeroWithoutBeasts("Eddard", "Stark", castleWinterfell, children);
//WHEN
WriteResult insert = heroes.insert(eddardStark);
//THEN
Assertions.assertThat(insert.getError()).isNull();
}
示例10: shouldAddFieldToTheLightbringer
import com.mongodb.WriteResult; //导入依赖的package包/类
@Test
public void shouldAddFieldToTheLightbringer() {
//GIVEN
WeaponDetails details = new WeaponDetails("The one who pulls out this sword from fire will be named Lord's Chosen ...", "Azor Ahai");
//WHEN
WriteResult lightbringer = weapons.update("{_id: #}", "Lightbringer").with("{$set: {details: #}}", details);
//THEN
assertThat(lightbringer.getError()).isNull();
//AND WHEN
Sword sword = weapons.findOne("{_id: 'Lightbringer'}").as(Sword.class);
//THEN
assertThat(sword).isNotNull();
}
示例11: addCall
import com.mongodb.WriteResult; //导入依赖的package包/类
/**
* Add the specified mvc to the specified database
*
* @param dbSpecPath
* @param mvc
* @return
*/
static String addCall(String dbSpecPath, MongoVariantContext mvc) {
NA12878DBArgumentCollection args = new NA12878DBArgumentCollection(dbSpecPath);
String errorMessage = null;
NA12878KnowledgeBase kb = null;
try {
kb = new NA12878KnowledgeBase(null, args);
WriteResult wr = kb.addCall(mvc);
errorMessage = wr.getError();
} catch (Exception ex) {
errorMessage = ex.getMessage();
if (errorMessage == null) errorMessage = "" + ex;
} finally {
if (kb != null) kb.close();
}
return errorMessage;
}
示例12: doPut
import com.mongodb.WriteResult; //导入依赖的package包/类
/**
* Perform a single 'put' operation on the local object store.
*
* @param ref Object reference string of the object to be written.
* @param obj JSON string encoding the object to be written.
* @param collection Collection to put to.
*
* @return a ResultDesc object describing the success or failure of the
* operation.
*/
private ResultDesc doPut(String ref, String obj, DBCollection collection,
boolean requireNew)
{
String failure = null;
if (obj == null) {
failure = "no object data given";
} else {
try {
DBObject objectToWrite = jsonLiteralToDBObject(obj, ref);
if (requireNew) {
WriteResult wr = collection.insert(objectToWrite);
} else {
DBObject query = new BasicDBObject();
query.put("ref", ref);
collection.update(query, objectToWrite, true, false);
}
} catch (Exception e) {
failure = e.getMessage();
}
}
return new ResultDesc(ref, failure);
}
示例13: doUpdate
import com.mongodb.WriteResult; //导入依赖的package包/类
/**
* Perform a single 'update' operation on the local object store.
*
* @param ref Object reference string of the object to be written.
* @param version Expected version number of object before updating.
* @param obj JSON string encoding the object to be written.
* @param collection Collection to put to.
*
* @return an UpdateResultDesc object describing the success or failure of
* the operation.
*/
private UpdateResultDesc doUpdate(String ref, int version, String obj,
DBCollection collection)
{
String failure = null;
boolean atomicFailure = false;
if (obj == null) {
failure = "no object data given";
} else {
try {
DBObject objectToWrite = jsonLiteralToDBObject(obj, ref);
DBObject query = new BasicDBObject();
query.put("ref", ref);
query.put("version", version);
WriteResult result =
collection.update(query, objectToWrite, false, false);
if (result.getN() != 1) {
failure = "stale version number on update";
atomicFailure = true;
}
} catch (Exception e) {
failure = e.getMessage();
}
}
return new UpdateResultDesc(ref, failure, atomicFailure);
}
示例14: updateOrCreate
import com.mongodb.WriteResult; //导入依赖的package包/类
@Override
public boolean updateOrCreate(final Collection<MongoApproval> mongoApprovals) {
boolean result = true;
for (MongoApproval mongoApproval : mongoApprovals) {
final Update update = Update
.update("expiresAt", mongoApproval.getExpiresAt())
.addToSet("status", mongoApproval.getStatus())
.addToSet("lastModifiedAt",
mongoApproval.getLastUpdatedAt());
final WriteResult writeResult = mongoTemplate.upsert(
byUserIdAndClientIdAndScope(mongoApproval), update,
MongoApproval.class);
if (writeResult.getN() != 1) {
result = false;
}
}
return result;
}
示例15: storeToMongoDb
import com.mongodb.WriteResult; //导入依赖的package包/类
/**
* This method extracts database collection name and inserts passet object in
* database.
* @param object
* Object to insert in database
* @return inserted object
* @throws ObjectNotStoredException
*/
private FfmaDomainObject storeToMongoDb(FfmaDomainObject object)
throws ObjectNotStoredException {
// TODO: check if exists? last time ? etc
DBCollection mongoCollection = db.getCollectionFromString(object
.getClass().getSimpleName());
WriteResult res = mongoCollection.insert((BasicDBObject) object);
log.debug("storeToMongoDb() coll:" + mongoCollection + ", res: " + res.toString());
try {
return retrieveObject(object);
} catch (Exception e) {
throw new ObjectNotStoredException(
"Cannot store and retreive object from db after creation!",
e);
}
}