本文整理汇总了Java中org.springframework.data.mongodb.core.query.Update.inc方法的典型用法代码示例。如果您正苦于以下问题:Java Update.inc方法的具体用法?Java Update.inc怎么用?Java Update.inc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.data.mongodb.core.query.Update
的用法示例。
在下文中一共展示了Update.inc方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: getID
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
@Override
public int getID(String indexKey, int defaultIndex) {
synchronized (IDGeneratorDaoImpl.class) {
if (!this.mongoBaseDao.collectionExists(IDGenerator.class)) {
this.mongoBaseDao.createCollection(IDGenerator.class);
}
Query query = new Query();
query.addCriteria(new Criteria("id").is(indexKey));
Update update = new Update();
update.inc("index", 1);
IDGenerator idGenerator = this.mongoBaseDao.findAndModify(query, update, IDGenerator.class);
if (idGenerator == null) {// 如果不存在,则新增
idGenerator = new IDGenerator(indexKey, defaultIndex);
this.mongoBaseDao.save(idGenerator);
}
return idGenerator.getIndex();
}
}
示例3: updateCashBalance
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的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;
}
示例4: nextSequeceNumber
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
private ShipmentSequence nextSequeceNumber(ShipmentType shipmentType) {
ShipmentSequence shipmentSequence = null;
if(shipmentSequenceDAO.findByShipmentCode(shipmentType.getKey()) == null){
shipmentSequence = shipmentSequenceDAO.save(new ShipmentSequence(shipmentType));
} else {
Update updateOp = new Update();
updateOp.inc("nextNumber", 1);
final Query query = new Query();
query.addCriteria(where("shipmentCode").is(shipmentType.getKey()));
WriteResult writeResult = mongoTemplate.updateMulti(query, updateOp, ShipmentSequence.class);
if(writeResult.getN() == 1){
shipmentSequence = shipmentSequenceDAO.findByShipmentCode(shipmentType.getKey());
} else {
throw new IllegalStateException("next number failed");
}
}
return shipmentSequence;
}
示例5: getNextSequenceId
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
@Override
public long getNextSequenceId(String key) {
//get sequence id
Query query = new Query(Criteria.where("_id").is(key));
//increase sequence id by 1
Update update = new Update();
update.inc("seq", 1);
//return new increased id
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
SequenceId seqId =
mongoOperation.findAndModify(query, update, options, SequenceId.class);
//if no id, throws SequenceException
if (seqId == null) {
log.error("Unable to get sequence id for key: {}", key);
throw new SequenceException("Unable to get sequence id for key: " + key);
}
log.debug("Next sequendId: {}", seqId);
return seqId.getSeq();
}
示例6: newRevisionId
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
public Integer newRevisionId() {
Query query = new Query();
query.addCriteria(Criteria.where("platformVersionId").is(PlatformServerConfig.PLATFORM_VERSION));
Update update = new Update();
update.inc("currentTopRevisionId", 1);
PlatformVersions platformVersions = platformVersionsDao.update(query, update);
return platformVersions.getCurrentTopRevisionId();
}
示例7: updateSequence
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
public void updateSequence(String key, Long delta) {
Query query = new Query(Criteria.where("id").is(key));
Update update = new Update();
update.inc("sequence", delta);
FindAndModifyOptions options = new FindAndModifyOptions();
options.upsert(true);
mongoOperations.findAndModify(query, update, options, Sequence.class);
}
示例8: getNextSequenceId
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
@Override
public int getNextSequenceId(String key) throws DataAccessException {
//get sequence id
Query query = new Query(Criteria.where("id").is(key));
//increase sequence id by 1
Update update = new Update();
update.inc("seq", 1);
//return new increased id
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
//this is the magic happened.
Sequence seqId = mongoOperations.findAndModify(query, update, options, Sequence.class);
// if no id, throws SequenceException
// optional, just a way to tell user when the sequence id is failed to
// generate.
if (seqId == null) {
seqId = new Sequence();
seqId.setId(key);
seqId.setSeq(1);
mongoOperations.insert(seqId);
return 1;
}else{
return seqId.getSeq();
}
}
示例9: getNextSequence
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
@Override
public SysSequence getNextSequence(String colName) {
Query query = new Query(Criteria.where("colName").is(colName));
Update update = new Update();
update.inc("sequence",1);
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
return mongoTemplate.findAndModify(query, update, options, SysSequence.class, Constant.COL_NAME_SYS_SEQUENCE);
}
示例10: getNextSequenceId
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
@Override
public long getNextSequenceId(String key) throws SequenceException {
//get sequence id
Query query = new Query(Criteria.where("_id").is(key));
//increase sequence id by 1
Update update = new Update();
update.inc("seq", 1);
//return new increased id
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
//this is the magic happened.
SequenceId seqId =
mongoOperation.findAndModify(query, update, options, SequenceId.class);
//if no id, throws SequenceException
//optional, just a way to tell user when the sequence id is failed to generate.
if (seqId == null) {
throw new SequenceException("Unable to get sequence id for key : " + key);
}
return seqId.getSeq();
}
示例11: testUpdateUser
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
/**
* 更新用户数据
*/
@Test
public void testUpdateUser() {
// update(query,update,class)
// Query query:需要更新哪些用户,查询参数
// Update update:操作符,需要对数据做什么更新
// Class class:实体类
// 更新age大于24的用户信息
Query query = new Query();
query.addCriteria(where("age").gt(24));
Update update = new Update();
// age值加2
update.inc("age", 2);
// update.set("name", "zhangsan"); 直接赋值
// update.unset("name"); 删去字段
// update.push("interest", "java"); 把java追加到interest里面,interest一定得是数组
// update.pushAll("interest", new String[]{".net","mq"})
// 用法同push,只是pushAll一定可以追加多个值到一个数组字段内
// update.pull("interest", "study"); 作用和push相反,从interest字段中删除一个等于value的值
// update.pullAll("interest", new String[]{"sing","dota"})作用和pushAll相反
// update.addToSet("interest", "study") 把一个值添加到数组字段中,而且只有当这个值不在数组内的时候才增加
// update.rename("oldName", "newName") 字段重命名
// 只更新第一条记录,age加2,name值更新为zhangsan
mongoTemplate.updateFirst(query, new Update().inc("age", 2).set("name", "zhangsan"), User.class);
// 批量更新,更新所有查询到的数据
mongoTemplate.updateMulti(query, update, User.class);
}
示例12: getNextSequenceId
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
@Override
public String getNextSequenceId(String key) throws SequenceException {
// get sequence id
Query query = new Query(Criteria.where("_id").is(key));
// increase sequence id by 1
Update update = new Update();
update.inc("seq", 1);
// return new increased id
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
if (mongoOperation != null) {
System.out.println(" Mongo Operations Found ");
// this is the magic happened.
SequenceId seqId = mongoOperation.findAndModify(query, update, options, SequenceId.class);
// if no id, throws SequenceException
// optional, just a way to tell user when the sequence id is failed to
// generate.
if (seqId == null) {
throw new SequenceException("Unable to get sequence id for key : " + key);
}
return seqId.getSeq();
} else {
System.out.println("No Mongo Operations Object .. returning 100");
return "100";
}
}
示例13: updateCashBalance
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
public boolean updateCashBalance(String officeId, double cashBalance) {
Update updateOp = new Update();
updateOp.inc("cashBalance", cashBalance);
final Query query = new Query();
query.addCriteria(where("_id").is(officeId));
WriteResult writeResult = mongoTemplate.updateMulti(query, updateOp, BranchOffice.class);
return writeResult.getN() == 1;
}
示例14: getNextSequence
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
/**
* 차기 SEQUENCE를 가져온다.
*
* @param name 게시판 ID
* @return 다음 글번호
*/
public Integer getNextSequence(String name) {
Integer nextSeq = 1;
Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
Update update = new Update();
update.inc("seq", 1);
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
Sequence sequence = mongoTemplate.findAndModify(query, update, options, Sequence.class);
if (sequence == null) {
Sequence newSequence = new Sequence();
newSequence.setName(name);
sequenceRepository.save(newSequence);
log.debug("sequence is Null. Insert new Sequence.");
return nextSeq;
} else {
nextSeq = sequence.getSeq();
return nextSeq;
}
}
示例15: increaseFieldValue
import org.springframework.data.mongodb.core.query.Update; //导入方法依赖的package包/类
@Override
public <T> void increaseFieldValue(Class<T> entityName, String field, Integer defaultValue, Serializable id) {
Query query = Query.query(Criteria.where("_id").is(id));
Update update = new Update();
update.inc(field, defaultValue);
operations.updateFirst(query, update, entityName);
}