本文整理汇总了Java中com.mongodb.WriteResult.getN方法的典型用法代码示例。如果您正苦于以下问题:Java WriteResult.getN方法的具体用法?Java WriteResult.getN怎么用?Java WriteResult.getN使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.WriteResult
的用法示例。
在下文中一共展示了WriteResult.getN方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
}
示例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: 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;
}
}
示例6: 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()));
}
}
示例7: 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);
}
示例8: 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;
}
示例9: getUpdateResult
import com.mongodb.WriteResult; //导入方法依赖的package包/类
/**
*
* mongodb,解析 更新操作是否成功
*
* 返回更新数据库的结果
*
* 小于零:更新出现异常 等于零:成功执行了更新的SQL,但是没有影响到任何数据 大于零:成功执行了更新的SQL,影响到多条数据,条数就是返回值
*
* @param result
* @return
*/
@Override
public int getUpdateResult(WriteResult result) {
if (result == null) {
return FAIL_CODE_ONE;
}
@SuppressWarnings("deprecation")
CommandResult cr = result.getLastError();
if (cr == null) {
return FAIL_CODE_TWO;
}
boolean error_flag = false;
error_flag = cr.ok();
if (!error_flag) {// 获取上次操作结果是否有错误.
return FAIL_CODE_THREE;
}
int affect_count = result.getN();// 操作影响的对象个数
if (affect_count < 0) {
return FAIL_CODE_FOUR;
} else {
return affect_count;
}
}
示例10: deleteProcess
import com.mongodb.WriteResult; //导入方法依赖的package包/类
/**
* Removes the selected process' metadata object.
*
* @param id the Id of the selected process' metadata object.
*/
public void deleteProcess(String id) {
log.debug(MSG_DAO_DELETE + id);
// Check passed Id
if (!ObjectId.isValid(id)) {
log.error(MSG_ERR_NOT_VALID_ID);
throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
}
processesCollection = INSTANCE.getDatasource().getDbCollection(PROCESSES_COLLECTION_NAME);
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
WriteResult wRes = processesCollection.remove(query);
// Check the number of deleted objects
if (wRes.getN() == 0) { // if 0 then the query found nothing
log.error(MSG_ERR_NOT_FOUND);
throw new ResourceNotFoundException(MSG_ERR_NOT_FOUND);
}
}
示例11: deleteUser
import com.mongodb.WriteResult; //导入方法依赖的package包/类
/**
* Remove the selected user.
*
* @param id the Id of the selected user.
*/
public void deleteUser(String id) {
log.debug(MSG_DAO_DELETE + id + ".");
if (!ObjectId.isValid(id)) {
log.error(MSG_ERR_NOT_VALID_ID);
throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
}
usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
WriteResult wRes = usersCollection.remove(query);
if (wRes.getN() == 0) {
log.error(MSG_ERR_NOT_FOUND);
throw new ResourceNotFoundException();
}
}
示例12: deleteDataset
import com.mongodb.WriteResult; //导入方法依赖的package包/类
/**
* Removes the selected dataset's metadata.
*
* @param id the Id of the selected dataset's metadata.
*/
public void deleteDataset(String id) {
log.debug(MSG_DAO_DELETE);
if (!ObjectId.isValid(id)) {
log.error(MSG_ERR_NOT_VALID_ID);
throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
}
datasetsCollection = INSTANCE.getDatasource().getDbCollection(DATASETS_COLLECTION_NAME);
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
WriteResult wRes = datasetsCollection.remove(query);
if (wRes.getN() == 0) {
log.error(MSG_ERR_NOT_FOUND);
throw new ResourceNotFoundException();
}
}
示例13: deleteDepartment
import com.mongodb.WriteResult; //导入方法依赖的package包/类
/**
* Remove the selected department.
*
* @param id the Id of the selected department.
*/
public void deleteDepartment(String id) {
log.debug(MSG_DAO_DELETE + id + ".");
if (!ObjectId.isValid(id)) {
log.error(MSG_ERR_NOT_VALID_ID);
throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
}
departmentsCollection = INSTANCE.getDatasource().
getDbCollection(DEPARTMENTS_COLLECTION_NAME);
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
WriteResult wRes = departmentsCollection.remove(query);
if (wRes.getN() == 0) {
log.error(MSG_ERR_NOT_FOUND);
throw new ResourceNotFoundException();
}
}
示例14: updateServiceAmenities
import com.mongodb.WriteResult; //导入方法依赖的package包/类
/**
* Update amenityIds for service(s). The List<JSONObject> should in the below format
* [{
* "serviceId":"1234", "amenityIds":["2323","33423","33523"]
* },{
* "serviceId":"1434", "amenityIds":["233433","3333423"]
* }]
*
* @return
*/
public boolean updateServiceAmenities(List<JSONObject> services) {
/*
BulkOperations ops = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, BusService.class);
for (JSONObject service: services) {
Query query = new Query(where(AbstractDocument.KEY_ID).is(service.get("serviceId").toString()));
Update updateOp = new Update();
updateOp.set("amenityIds", service.get("amenityIds"));
ops.updateOne(query, updateOp);
}
BulkWriteResult result = ops.execute();
return result.getModifiedCount() == services.size(); */
for (JSONObject jsonObject : services) {
Update updateOp = new Update();
updateOp.set("amenityIds", jsonObject.get("amenityIds"));
final Query query = new Query();
query.addCriteria(where("_id").is(jsonObject.get("serviceId")));
WriteResult writeResult = mongoTemplate.updateMulti(query, updateOp, BusService.class);
if(writeResult.getN() != 1) {
return false;
}
}
return true;
}
示例15: updateCashBalance
import com.mongodb.WriteResult; //导入方法依赖的package包/类
public boolean updateCashBalance(String userId, double cashBalance) {
Update updateOp = new Update();
updateOp.inc("amountToBePaid", cashBalance);
final Query query = new Query();
query.addCriteria(where("_id").is(userId));
WriteResult writeResult = mongoTemplate.updateMulti(query, updateOp, User.class);
return writeResult.getN() == 1;
}