本文整理汇总了Java中org.springframework.data.mongodb.core.FindAndModifyOptions类的典型用法代码示例。如果您正苦于以下问题:Java FindAndModifyOptions类的具体用法?Java FindAndModifyOptions怎么用?Java FindAndModifyOptions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FindAndModifyOptions类属于org.springframework.data.mongodb.core包,在下文中一共展示了FindAndModifyOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNextSequenceId
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的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: poll
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的package包/类
@Override
public Optional<T> poll() {
LocalDateTime expiredheartbeatDate = LocalDateTime
.now()
.minus(defaultHeartbeatExpirationMillis, ChronoUnit.MILLIS);
Criteria mainOr = generatePollCriteria(expiredheartbeatDate);
Update update = Update
.update("status", OkraStatus.PROCESSING)
.set("heartbeat", LocalDateTime.now());
Query query = Query.query(mainOr);
FindAndModifyOptions opts = new FindAndModifyOptions()
.returnNew(true);
return Optional.ofNullable(mongoTemplate.findAndModify(query, update, opts, scheduleItemClass));
}
示例3: lock
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的package包/类
@Override
public void lock(String business_type, String lock_id) throws BusinessError {
try {
if(isDev){//开发状态就不用了吧
return ;
}
//修改一下做法,改用
Criteria c = Criteria.where("business").is(business_type).and("lock_id").is(lock_id);//使用bill_code+sn_len+yyyymmdd进行查询定位
Update update = new Update().inc("seed", 1);//自增
FindAndModifyOptions opt = FindAndModifyOptions.options().returnNew(true).upsert(true);
JSONObject rs = operations.findAndModify(Query.query(c), update, opt,JSONObject.class,COLL_LOCK);
Integer current_value = rs.getInteger("seed");//新的单据号值
if(current_value >1){
ErrorBuilder.createBusiness().msg("正在处理,请稍后再试").execute();
}
} catch (Exception e) {
ErrorBuilder.createBusiness().cause(e).execute();
}
}
示例4: getNextSequenceId
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的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();
}
示例5: updateCount
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的package包/类
public void updateCount() {
MapIt updatedMapItRecord = mongoTemplate.findAndModify(new Query(Criteria.where("_id")
.is(MAPIT_ID)), new Update().inc("hitcount", 1),
new FindAndModifyOptions().returnNew(true),
MapIt.class,
"mapit");
DateTime now = new DateTime();
DateTime startDate = new DateTime(updatedMapItRecord.getStartdate());
Period period = new Period(startDate, now);
String elapsed = formatter.print(period);
if ( (elapsed.equals("")) && (updatedMapItRecord.getHitcount() >= LIC_ALERT)) {
//we have almost reached the ceiling within this year!
logger.warn("***Reaching max hits ceiling for this year, current count:"
+updatedMapItRecord.getHitcount());
}
}
示例6: updateSequence
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的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);
}
示例7: upsertViewList
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的package包/类
@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));
}
}
});
}
示例8: getNextSequenceId
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的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.FindAndModifyOptions; //导入依赖的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.FindAndModifyOptions; //导入依赖的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: heartbeatAndUpdateCustomAttrs
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的package包/类
@Override
public Optional<T> heartbeatAndUpdateCustomAttrs(T item, Map<String, Object> attrs) {
if (item.getId() == null
|| item.getHeartbeat() == null
|| item.getStatus() == null) {
return Optional.empty();
}
Criteria criteria =
Criteria.where("_id").is(new ObjectId(item.getId()))
.and("status").is(OkraStatus.PROCESSING)
.and("heartbeat").is(item.getHeartbeat());
Query query = Query.query(criteria);
Update update = Update.update("heartbeat", LocalDateTime.now());
if (attrs != null && !attrs.isEmpty()) {
attrs.forEach(update::set);
}
FindAndModifyOptions opts = new FindAndModifyOptions()
.returnNew(true);
LOGGER.info("Querying for schedules using query: {}", query);
return Optional.ofNullable(mongoTemplate.findAndModify(query, update, opts, scheduleItemClass));
}
示例12: getNextSequenceId
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的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: updateByTitle
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public Book updateByTitle(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);
}
示例14: onApplicationEvent
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的package包/类
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof AuthenticationSuccessEvent) {
AuthenticationSuccessEvent successEvent =
(AuthenticationSuccessEvent) event;
final Authentication authentication = successEvent.getAuthentication();
final OpenAMUserdetails details = (OpenAMUserdetails) authentication.getDetails();
final String nickname = details.getUsername();
Query query = new Query();
query.addCriteria(Criteria.where("nickName").is(nickname));
final String surname = details.getAttributeValue("sn");
final String givenname = details.getAttributeValue("givenname");
final String email = details.getAttributeValue("mail");
Update update = new Update();
update.set("email", email);
update.set("givenName", givenname);
update.set("surName", surname);
update.set("lastLogin", new Date());
try {
update.set("gravatar", User.md5Hex(email));
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
LOGGER.error("Failed while calculating gravatar hash!");
}
final User user = mongoTemplate.findAndModify(query, update, FindAndModifyOptions.options().upsert(true).returnNew(true), User.class);
if (user != null) {
LOGGER.debug("LoginEvent for user: " + user.toString());
} else {
LOGGER.debug("Upsert didn't return new user!");
}
}
}
示例15: testGetNextSequenceId
import org.springframework.data.mongodb.core.FindAndModifyOptions; //导入依赖的package包/类
@Test
public void testGetNextSequenceId() {
when(mongoOperation.findAndModify(any(Query.class), any(Update.class), any(FindAndModifyOptions.class),
eq(SequenceId.class))).thenReturn(new SequenceId().setId(BookServiceImpl.BOOK_COLLECTION).setSeq(6L));
long seqId = repository.getNextSequenceId(BookServiceImpl.BOOK_COLLECTION);
verify(mongoOperation, times(ONE_TIME)).findAndModify(any(Query.class), any(Update.class),
any(FindAndModifyOptions.class), eq(SequenceId.class));
assertTrue("Debe devolver un sequence valido", seqId > 0);
}