org.springframework.data.mongodb.core.FindAndModifyOptions Java Examples

The following examples show how to use org.springframework.data.mongodb.core.FindAndModifyOptions. 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 vote down vote up
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 vote down vote up
/**
 * 获取下一个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 vote down vote up
/**
 * 获取下一个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 vote down vote up
@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: SimpleMongoLock.java    From distributed-lock with MIT License 5 votes vote down vote up
@Override
protected String acquire(final String key, final String storeId, final String token, final long expiration) {
  final var query = Query.query(Criteria.where("_id").is(key));
  final var update = new Update()
    .setOnInsert("_id", key)
    .setOnInsert("expireAt", LocalDateTime.now().plus(expiration, ChronoUnit.MILLIS))
    .setOnInsert("token", token);

  final var options = new FindAndModifyOptions().upsert(true).returnNew(true);
  final var doc = mongoTemplate.findAndModify(query, update, options, LockDocument.class, storeId);

  final var locked = doc.getToken().equals(token);
  log.debug("Tried to acquire lock for key {} with token {} in store {}. Locked: {}", key, token, storeId, locked);
  return locked ? token : null;
}
 
Example #6
Source File: MetricsFunction.java    From service-block-samples with Apache License 2.0 5 votes vote down vote up
@Transactional
private void upsertViewList(List<View> viewList, MongoTemplate template, Map<String, Object> result) {
    viewList.forEach(view -> {
        // Find the document if it exists, if not, insert a new one
        Query updateQuery = new Query(Criteria.where("_id").is(view.getId()));

        // Increment the match count for the coupled files
        Update update = new Update().inc("matches", 1).set("lastModified", view.getLastModified());

        // Apply the increment or insert a new document
        View viewResult = template.findAndModify(updateQuery, update,
                new FindAndModifyOptions().returnNew(true).upsert(true), View.class);

        // Apply properties of a new view if the document was just inserted
        if (viewResult.getMatches() <= 1) {
            template.save(view);
            // Keep track of inserts and updates
            ((List<String>) result.get("inserted")).add(view.getId());
        } else {
            ((List<String>) result.get("updated")).add(view.getId());
            if(viewResult.getMatches() >= 2) {
                ((List<TightCouplingEvent>) result.get("events"))
                        .add(new TightCouplingEvent(view.getProjectId(), viewResult));
            }
        }
    });
}
 
Example #7
Source File: SequenceDaoImpl.java    From microservices-sample-project with Apache License 2.0 5 votes vote down vote up
@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 #8
Source File: CommonDaoImpl.java    From ProxyPool with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: BookServiceImpl.java    From POC with Apache License 2.0 5 votes vote down vote up
@Override
@CachePut(value = "book", key = "#title")
public Book updateAuthorByTitle(String title, String author) {
	log.info("Updating Book Author by Title :{} with {}", title, author);
	final Query query = new Query(Criteria.where("title").is(title));
	final Update update = new Update().set("author", author);
	return this.mongoTemplate.findAndModify(query, update, new FindAndModifyOptions().returnNew(true).upsert(false),
			Book.class);
}
 
Example #10
Source File: CommonService.java    From jakduk-api with MIT License 5 votes vote down vote up
/**
 * 차기 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 #11
Source File: MongoEntityRepository.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
@MigrateEntity
public Entity findAndUpdate(String collectionName, NeutralQuery neutralQuery, Update update) {
    Query query = this.getQueryConverter().convert(collectionName, neutralQuery);
    FindAndModifyOptions options = new FindAndModifyOptions();
    Entity result = template.findAndModify(query, update, options, getRecordClass(), collectionName);

    if (result != null
            && collectionName.equals(EntityNames.EDUCATION_ORGANIZATION)) {
        updateAllSchoolLineage();
    }

    return result;
}
 
Example #12
Source File: ProductRepositoryImpl.java    From vladmihalcea.wordpress.com with Apache License 2.0 5 votes vote down vote up
@Override
public Product findAndInsert(Long id) {
    return mongoTemplate.findAndModify(
            new Query(where(Properties.ID).is(id)),
            Update.update(Properties.ID, id),
            FindAndModifyOptions.options().upsert(true).returnNew(true),
            Product.class
    );
}