Java Code Examples for org.springframework.data.mongodb.core.query.Update#inc()
The following examples show how to use
org.springframework.data.mongodb.core.query.Update#inc() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SequenceOption.java From HA-DB with BSD 3-Clause "New" or "Revised" License | 6 votes |
private long generate(MongoTemplate template, String collectionName, String rowName, Long incrementVal) { Criteria criteria = Criteria.where(SequenceId.COLLNAME).is(collectionName); if (rowName != null) { criteria.and(SequenceId.ROW).is(rowName); } else { criteria.and(SequenceId.ROW).ne("").ne(null); } Query query = new Query(criteria); Update update = new Update(); update.inc(SequenceId.SEQ, incrementVal); FindAndModifyOptions options = new FindAndModifyOptions(); options.upsert(false); // 不做插入,所有的自增键由表维护 options.returnNew(true); SequenceId seqId = template.findAndModify(query, update, options, SequenceId.class, SequenceId.SEQUENCE_ID_COL_NAME); return seqId.getSeq(); }
Example 2
Source File: IdUtil.java From microservice-recruit with Apache License 2.0 | 5 votes |
/** * 获取下一个id并更新表 * @param collName * @param mongo * @return */ public static Long getNextIdAndUpdate(String collName, MongoTemplate mongo) { Query query = new Query(Criteria.where("collName").is(collName)); Update update = new Update(); update.inc("seqId", 1); FindAndModifyOptions options = new FindAndModifyOptions(); options.upsert(true); options.returnNew(true); SeqInfo seq = mongo.findAndModify(query, update, options, SeqInfo.class); return seq.getSeqId(); }
Example 3
Source File: IdUtil.java From microservice-recruit with Apache License 2.0 | 5 votes |
/** * 获取下一个id并更新表 * @param collName * @param mongo * @return */ public static Long getNextIdAndUpdate(String collName, MongoTemplate mongo) { Query query = new Query(Criteria.where("collName").is(collName)); Update update = new Update(); update.inc("seqId", 1); FindAndModifyOptions options = new FindAndModifyOptions(); options.upsert(true); options.returnNew(true); SeqInfo seq = mongo.findAndModify(query, update, options, SeqInfo.class); return seq.getSeqId(); }
Example 4
Source File: SequenceDaoImpl.java From spring-cloud-demo with Apache License 2.0 | 5 votes |
@Override public Long getNextSequenceId(String key) { Query query = new Query(Criteria.where("id").is(key)); Update update = new Update(); update.inc("sequenceNo", 1); FindAndModifyOptions options = new FindAndModifyOptions(); options.returnNew(true); Sequence seqId = mongoOperation.findAndModify(query, update, options, Sequence.class); return seqId.getSequenceNo(); }
Example 5
Source File: SequenceDaoImpl.java From microservices-sample-project with Apache License 2.0 | 5 votes |
@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(); } }
Example 6
Source File: CommonDaoImpl.java From ProxyPool with Apache License 2.0 | 5 votes |
@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); }
Example 7
Source File: ArticleRepositoryTest.java From spring-boot-demo with MIT License | 5 votes |
/** * 测试点赞数、访客数,使用更优雅/高效的方式更新点赞、访客 */ @Test public void testThumbUp2() { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(1L)); Update update = new Update(); update.inc("thumbUp", 1L); update.inc("visits", 1L); mongoTemplate.updateFirst(query, update, "article"); articleRepo.findById(1L) .ifPresent(article -> log.info("【标题】= {}【点赞数】= {}【访客数】= {}", article.getTitle(), article.getThumbUp(), article .getVisits())); }
Example 8
Source File: CommonService.java From jakduk-api with MIT License | 5 votes |
/** * 차기 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; } }
Example 9
Source File: MongoGenericDao.java From howsun-javaee-framework with Apache License 2.0 | 5 votes |
@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); }