本文整理匯總了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);
}
}