Java Code Examples for org.springframework.data.mongodb.core.query.Query#query()

The following examples show how to use org.springframework.data.mongodb.core.query.Query#query() . 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: TxManagerInfoServiceImpl.java    From Lottor with MIT License 6 votes vote down vote up
private Long countTxMsgs(Timestamp from, Timestamp to) {
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String toStr = "";
    String fromStr = "";
    try {
        toStr = sdf.format(to);
        fromStr = sdf.format(from);
    } catch (Exception e) {
        LogUtil.error(LOGGER, "日期转换错误,cause is {}", e::getLocalizedMessage);
        throw new IllegalArgumentException("请检查传入的日期参数!");
    }
    Criteria criteria = Criteria.where("createDate").lte(toStr).gte(fromStr);
    Query query = Query.query(criteria);
    return mongoTemplate.count(query, TxTransactionItem.class, CollectionNameEnum.TxTransactionItem.name());

}
 
Example 2
Source File: TransitiveStudentToStaffValidator.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
protected Set<String> filterConnectedViaSection(Set<String> ids, Entity me) {
    if (ids.isEmpty()) {
        return ids;
    }
    //The fact that this goes through a subdoc on section means I can't reuse filterThroughSubdocs without adding another couple dozen parameters to that method...
    Set<String> sectionIds = getDenormIds(me, "section", "_id");
    if (sectionIds.isEmpty()) {
        return Collections.emptySet();
    }
    Query q = Query.query(Criteria.where("_id").in(sectionIds).and("teacherSectionAssociation").elemMatch(Criteria.where("body.teacherId").in(ids).andOperator(DateHelper.getExpiredCriteria())));
    q.fields().include("teacherSectionAssociation.$");
    Iterator<Entity> sections = getRepo().findEach(EntityNames.SECTION, q);
    Set<String> filtered = new HashSet<String>();
    while (sections.hasNext()) {
        Entity section = sections.next();
        List<Entity> tsas = section.getEmbeddedData().get("teacherSectionAssociation");
        if (tsas != null) {
            for (Entity tsa : tsas) {
                String teacher = (String) tsa.getBody().get("teacherId");
                filtered.add(teacher);
            }
        }
    }
    return filtered;
}
 
Example 3
Source File: AuthorUtil.java    From biliob_backend with MIT License 5 votes vote down vote up
public void getInterval(List<Author> authors) {
    List<Long> midList = authors.stream().map(Author::getMid).collect(Collectors.toList());
    Query q = Query.query(Criteria.where("mid").in(midList));
    Map<Long, Integer> intervalMap = mongoTemplate.find(q, AuthorIntervalRecord.class)
            .stream().collect(Collectors.toMap(AuthorIntervalRecord::getMid, AuthorIntervalRecord::getInterval));
    for (Author author : authors
    ) {
        author.setObInterval(intervalMap.get(author.getMid()));
    }
}
 
Example 4
Source File: MongoClientDetailsRepositoryImpl.java    From spring-security-mongo with MIT License 5 votes vote down vote up
@Override
public boolean updateClientSecret(final String clientId,
                                  final String newSecret) {
    final Query query = Query.query(Criteria.where(ID).is(clientId));

    final Update update = Update.update(CLIENT_SECRET, newSecret);

    final UpdateResult updateResult = mongoTemplate.updateFirst(query, update, MongoClientDetails.class);

    return updateResult.wasAcknowledged();
}
 
Example 5
Source File: FlowUserDaoImpl.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(String flowId, Set<String> userIds) {
    Query q = Query.query(Criteria.where("_id").is(flowId));

    Update u = new Update();
    u.pullAll("users", userIds.toArray());

    mongoOps.updateFirst(q, u, FlowUsers.class);
}
 
Example 6
Source File: FlowUserDaoImpl.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> findAllFlowsByUserEmail(String email) {
    Query q = Query.query(Criteria.where("users").in(email));
    q.fields().exclude("users");

    List<FlowUsers> lists = mongoOps.find(q, FlowUsers.class);
    List<String> ids = new LinkedList<>();

    for (FlowUsers item : lists) {
        ids.add(item.getFlowId());
    }

    return ids;
}
 
Example 7
Source File: MongoTransactionManager.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
protected void unlock(TransactionDefinition definition) {
    Instant now = Instant.now();
    Criteria criteria = Criteria.where(MongoMetadata.mongoId).is(definition.getName());
    Criteria[] andCriterias = new Criteria[2];
    andCriterias[0] = Criteria.where("most").is(definition.getMost().toEpochMilli());
    andCriterias[1] = Criteria.where("most").gt(now.toEpochMilli());
    Query query = Query.query(criteria.andOperator(andCriterias));
    Update update = new Update();
    update.set("most", now.toEpochMilli());
    long count = accessor.update(MongoTransactionDefinition.class, query, update);
    if (count != 1) {
        throw new TransactionUnlockException();
    }
}
 
Example 8
Source File: SpiderScheduler.java    From biliob_backend with MIT License 5 votes vote down vote up
@Async
private void keepMostViewVideoInterval() {
    Query q = Query.query(Criteria.where("cView").gt(5000000));
    q.fields().include("aid");
    List<Video> v = mongoTemplate.find(q, Video.class);
    for (Video video : v
    ) {
        Long aid = video.getAid();
        VideoIntervalRecord vir = mongoTemplate.findOne(Query.query(Criteria.where("aid").is(aid)), VideoIntervalRecord.class);
        if (vir == null || vir.getNext() == null || vir.getDate() == null) {
            mongoTemplate.upsert(Query.query(Criteria.where("aid").is(aid)), Update.update("interval", SECOND_OF_DAY).set("next", Calendar.getInstance().getTime()).set("date", Calendar.getInstance().getTime()), VideoIntervalRecord.class);
            logger.info("每日更新av{}", aid);
        }
    }
}
 
Example 9
Source File: MongoRepository.java    From flash-waimai with MIT License 5 votes vote down vote up
public <T> Page<T> queryPage(Page<T> page, Class<T> klass, Map<String, Object> params) {
    Pageable pageable = PageRequest.of(page.getCurrent() - 1, page.getSize(), Sort.Direction.DESC, "id");
    Query query = new Query();
    if (params != null && !params.isEmpty()) {
        Criteria criteria = criteria(params);
        query = Query.query(criteria);
    }
    List<T> list = mongoTemplate.find(query.with(pageable), klass);
    Long count = count(klass, params);
    page.setTotal(count.intValue());
    page.setRecords(list);
    return page;
}
 
Example 10
Source File: FlowUserDaoImpl.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(String flowId, Set<String> emails) {
    Query q = Query.query(Criteria.where("_id").is(flowId));

    Update u = new Update();
    u.pullAll("users", emails.toArray());

    mongoOps.updateFirst(q, u, FlowUsers.class);
}
 
Example 11
Source File: SysUserServiceTenantTest.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 查询所有符合条件的数据,返回List
 */
@Test
public void findByTenantCodeTest(){
    Query query = Query.query(Criteria.where("tenant_code").is(null));
    List<SysUser> sysUsers = mongoTemplate.find(query, SysUser.class);
    log.info("find 影响行数:"+sysUsers.size());
    log.info("find 影响数据:"+JSONUtil.toJsonStr(sysUsers));
}
 
Example 12
Source File: SysUserServiceTenantTest.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 更新多条
 */
@Test
public void updateOneTest(){
    Query query = Query.query(Criteria.where("tenant_code").is(null));
    Update update = Update.update("tenant_code", "XYS");
    UpdateResult updateResult = mongoTemplate.updateFirst(query, update, SysUser.class);
    log.info("update 影响行数:"+updateResult.getModifiedCount());
}
 
Example 13
Source File: MongoGenericDao.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 14
Source File: BaseService.java    From HA-DB with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public boolean delete(String collectionName, String id) {
	Query query = Query.query(Criteria.where(Opations.ID_FIELD).is(id));
	DeleteResult result = mongoTemplate.remove(query, collectionName);
	return result.getDeletedCount() > 0;
}
 
Example 15
Source File: MongoClientDetailsRepositoryImpl.java    From spring-security-mongo with MIT License 4 votes vote down vote up
@Override
public boolean deleteByClientId(String clientId) {
    final Query query = Query.query(Criteria.where(ID).is(clientId));
    final DeleteResult deleteResult = mongoTemplate.remove(query, MongoClientDetails.class);
    return deleteResult.wasAcknowledged();
}
 
Example 16
Source File: MongoOAuth2AccessTokenRepositoryImpl.java    From spring-security-mongo with MIT License 4 votes vote down vote up
@Override
public MongoOAuth2AccessToken findByTokenId(final String tokenId) {
    final Query query = Query.query(Criteria.where(ID).is(tokenId));
    return mongoTemplate.findOne(query, MongoOAuth2AccessToken.class);
}
 
Example 17
Source File: AuthorServiceImpl.java    From biliob_backend with MIT License 4 votes vote down vote up
@Override
public List<Author> getAuthorFansGt(int gt) {
    Query q = Query.query(Criteria.where("cFans").gt(gt));
    q.fields().include("mid");
    return mongoTemplate.find(q, Author.class, "author");
}
 
Example 18
Source File: VideoServiceImpl.java    From biliob_backend with MIT License 4 votes vote down vote up
private List<Video> getVideoViewGt(int gt) {
    Query q = Query.query(Criteria.where("cView").gt(gt));
    q.fields().include("mid");
    return mongoTemplate.find(q, Video.class, "video");
}
 
Example 19
Source File: TransactionalWalletService.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 4 votes vote down vote up
private Query queryForOwner(String owner) {
   return Query.query(new Criteria("owner").is(owner));
}
 
Example 20
Source File: ContainerDocumentAccessor.java    From secure-data-service with Apache License 2.0 3 votes vote down vote up
private DBObject getContainerDocQuery(final Entity entity) {
    final String parentKey = ContainerDocumentHelper.createParentKey(entity, containerDocumentHolder, generatorStrategy);

    final Query query = Query.query(Criteria.where("_id").is(parentKey));

    return query.getQueryObject();
}