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

The following examples show how to use org.springframework.data.mongodb.core.query.Query#with() . 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: UserRepositoryImpl.java    From spring-backend-boilerplate with Apache License 2.0 6 votes vote down vote up
@Override
public Page<User> queryPage(SysRole role, UserQueryRequest queryRequest) {
	int start = queryRequest.getStart();
	int limit = queryRequest.getLimit();
	Query query = new Query();
	query.addCriteria(Criteria.where("roles").in(role));
	if (!StringUtils.isEmpty(queryRequest.getUsername())) {
		query.addCriteria(Criteria.where("username").regex(queryRequest.getUsername()));
	}
	query.addCriteria(Criteria.where("deleted").ne(true));

	PageRequest pageable = new PageRequest(start, limit, new Sort(Sort.Direction.DESC, "username"));
	query.with(pageable);
	long count = mongoTemplate.count(query, User.class);
	List<User> list = mongoTemplate.find(query, User.class);

	return new PageImpl<>(list, pageable, count);
}
 
Example 2
Source File: DocumentQueryLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenUsersExist_whenFindingByPage_thenUsersAreFoundByPage() {
    User user = new User();
    user.setName("Eric");
    user.setAge(45);
    mongoTemplate.insert(user);

    user = new User();
    user.setName("Antony");
    user.setAge(33);
    mongoTemplate.insert(user);

    user = new User();
    user.setName("Alice");
    user.setAge(35);
    mongoTemplate.insert(user);

    final Pageable pageableRequest = PageRequest.of(0, 2);
    Query query = new Query();
    query.with(pageableRequest);

    List<User> users = mongoTemplate.find(query, User.class);

    assertThat(users.size(), is(2));
}
 
Example 3
Source File: SimilarityManager.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public List<Artifact> getSimilarProjects(Artifact prj1, ISimilarityCalculator similarityCalculator, int numResult) {

	Query q1 = new Query(Criteria.where("type.name").is(similarityCalculator.getSimilarityName()).orOperator(
			Criteria.where("toArtifact.$id").is(new ObjectId(prj1.getId())),
			Criteria.where("fromArtifact.$id").is(new ObjectId(prj1.getId()))));
	q1.with(new Sort(Sort.Direction.DESC, "value"));
	q1.limit(numResult);
	List<Relation> r1 = mongoOperations.find(q1, Relation.class);
	List<Artifact> results = new ArrayList<>();
	for (Relation rel : r1) {
		if (rel.getFromProject().getId().equals(prj1.getId()))
			// results.add(rel.getToProject());
			results.add(artifactRepository.findOne(rel.getToProject().getId()));
		else
			// results.add(rel.getFromProject());
			results.add(artifactRepository.findOne(rel.getFromProject().getId()));
	}
	return results;
}
 
Example 4
Source File: EventMongoDBDaoImpl.java    From chronus with Apache License 2.0 6 votes vote down vote up
@Override
public List<EventEntity> getLastEvent(String cluster, String address, String version) {
    Query query = new Query();
    query.addCriteria(Criteria.where("cluster").is(cluster));
    query.addCriteria(Criteria.where("address").is(address));
    query.addCriteria(Criteria.where("version").is(version));
    query.with(new Sort(Sort.Direction.DESC, "dateCreated"));
    query.limit(3);
    return super.selectList(query);
}
 
Example 5
Source File: MongoTemplateQueryLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenUsersExist_whenFindingByPage_thenUsersAreFoundByPage() {
    User user = new User();
    user.setName("Eric");
    user.setAge(45);
    mongoTemplate.insert(user);

    user = new User();
    user.setName("Antony");
    user.setAge(33);
    mongoTemplate.insert(user);

    user = new User();
    user.setName("Alice");
    user.setAge(35);
    mongoTemplate.insert(user);

    final Pageable pageableRequest = PageRequest.of(0, 2);
    Query query = new Query();
    query.with(pageableRequest);

    List<User> users = mongoTemplate.find(query, User.class);
    assertThat(users.size(), is(2));
}
 
Example 6
Source File: ArticleRepositoryImpl.java    From jakduk-api with MIT License 6 votes vote down vote up
/**
 * 글 보기에서 앞 글, 뒷 글의 정보를 가져온다.
 */
@Override
public ArticleSimple findByIdAndCategoryWithOperator(ObjectId id, String category, Constants.CRITERIA_OPERATOR operator) {
    Query query = new Query();

    if (StringUtils.isNotBlank(category))
        query.addCriteria(Criteria.where("category").is(category));

    switch (operator) {
        case GT:
            query.addCriteria(Criteria.where("_id").gt(id));
            query.with(new Sort(Sort.Direction.ASC, "_id"));
            break;
        case LT:
            query.addCriteria(Criteria.where("_id").lt(id));
            query.with(new Sort(Sort.Direction.DESC, "_id"));
            break;
    }

    return mongoTemplate.findOne(query, ArticleSimple.class);
}
 
Example 7
Source File: MongoTemplateQueryLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenUsersExist_whenFindingUsersAndSortThem_thenUsersAreFoundAndSorted() {
    User user = new User();
    user.setName("Eric");
    user.setAge(45);
    mongoTemplate.insert(user);

    user = new User();
    user.setName("Antony");
    user.setAge(33);
    mongoTemplate.insert(user);

    user = new User();
    user.setName("Alice");
    user.setAge(35);
    mongoTemplate.insert(user);

    Query query = new Query();
    query.with(new Sort(Sort.Direction.ASC, "age"));

    List<User> users = mongoTemplate.find(query, User.class);
    assertThat(users.size(), is(3));
}
 
Example 8
Source File: UserRepositoryImpl.java    From spring-backend-boilerplate with Apache License 2.0 6 votes vote down vote up
@Override
public Page<User> queryPage(UsernameQueryRequest queryRequest) {
	int start = queryRequest.getStart();
	int limit = queryRequest.getLimit();
	Query query = new Query();
	if (!StringUtils.isEmpty(queryRequest.getUsername())) {
		query.addCriteria(Criteria.where("username").regex(queryRequest.getUsername()));
	}
	query.addCriteria(Criteria.where("deleted").ne(true));

	PageRequest pageable = new PageRequest(start, limit, new Sort(Sort.Direction.ASC, "rank", "username"));
	query.with(pageable);
	long count = mongoTemplate.count(query, User.class);
	List<User> list = mongoTemplate.find(query, User.class);

	return new PageImpl<>(list, pageable, count);
}
 
Example 9
Source File: ProxyDaoImpl.java    From ProxyPool with Apache License 2.0 5 votes vote down vote up
@Override
public List<ProxyDataDTO> findLimitProxy(int count) {
    Query query = new Query();
    query.limit(count);
    query.with(new Sort(Sort.Direction.DESC, "lastSuccessfulTime"));
    query.with(new Sort(Sort.Direction.ASC, "proxyPort"));
    return mongoTemplate.find(query, ProxyDataDTO.class,Constant.COL_NAME_PROXY);
}
 
Example 10
Source File: AuthEventRepositoryImpl.java    From spring-backend-boilerplate with Apache License 2.0 5 votes vote down vote up
@Override
public Page<AuthEvent> queryPage(AuthEventQueryRequest queryRequest) {

	int start = queryRequest.getStart();
	int limit = queryRequest.getLimit();
	Query query = buildQuery(queryRequest);

	PageRequest pageable = new PageRequest(start, limit, new Sort(Sort.Direction.DESC, "loginAt"));
	query.with(pageable);
	long count = mongoTemplate.count(query, AuthEvent.class);
	List<AuthEvent> list = mongoTemplate.find(query, AuthEvent.class);

	return new PageImpl<>(list, pageable, count);
}
 
Example 11
Source File: MongoAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> K maximumIdentity(Class<T> clazz, K from, K to) {
	MongoMetadata metadata = metadatas.get(clazz);
	Query query = Query.query(Criteria.where(MongoMetadata.mongoId).gte(from).lte(to));
	query.fields().include(MongoMetadata.mongoId);
	query.with(Sort.by(Direction.DESC, MongoMetadata.mongoId));
	T instance = template.findOne(query, clazz, metadata.getOrmName());
	return instance.getId();
}
 
Example 12
Source File: DataTablesCriteria.java    From spring-data-mongodb-datatables with Apache License 2.0 5 votes vote down vote up
private void addSort(Query query, DataTablesInput input) {
    query.skip(input.getStart());
    query.limit(input.getLength());

    if (isEmpty(input.getOrder())) return;

    List<Sort.Order> orders = input.getOrder().stream()
            .filter(order -> isOrderable(input, order))
            .map(order -> toOrder(input, order)).collect(toList());
    query.with(by(orders));
}
 
Example 13
Source File: EventMongoDBDaoImpl.java    From chronus with Apache License 2.0 5 votes vote down vote up
@Override
public List<EventEntity> getAllEvent(String cluster, String address, String version) {
    Query query = new Query();
    query.addCriteria(Criteria.where("cluster").is(cluster));
    query.addCriteria(Criteria.where("address").is(address));
    query.addCriteria(Criteria.where("version").is(version));
    query.with(new Sort(Sort.Direction.DESC, "dateCreated"));
    return super.selectList(query);
}
 
Example 14
Source File: TaskMongoDBDaoImpl.java    From chronus with Apache License 2.0 5 votes vote down vote up
@Override
public List<TaskEntity> selectTaskInfoByCluster(String cluster) {
    Query query = new Query();
    query.addCriteria(Criteria.where("cluster").is(cluster));
    query.with(new Sort(Sort.Direction.DESC, "dateUpdated"));
    return selectList(query);
}
 
Example 15
Source File: MongoBaseService.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * mongodb分页排序查询
 * @param pageParam:分页参数(pageNo、pageSize、order、direction)
 * @param query
 * @param cla
 * @param collectionName
 * @return
 */
public Pagenation page(PageParam pageParam , Query query, Class<T> cla, String collectionName ){
    if(pageParam.getOrders()!=null&&pageParam.getDirection()!=null) {
        query.with(new Sort(pageParam.getDirection(),pageParam.getOrders()));
    }
    long total  = mongoTemplate.count(query,cla,collectionName);
    query.limit(pageParam.getPageSize()).skip((pageParam.getPageNo()-1)*pageParam.getPageSize());
    List<T> list = mongoTemplate.find(query,cla,collectionName);
    long consult = total/pageParam.getPageSize() ;
    long residue = total%pageParam.getPageSize();
    long totalPage = residue == 0 ?consult :(consult+1) ;
    Pagenation pagenation = new Pagenation(pageParam);
    return pagenation.setData(list,total,totalPage);
}
 
Example 16
Source File: GenericService.java    From HA-DB with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public List<Docs> pageNoCount(String collectionName, Document queryDoc, Pageable pageable) {
	Query query = new BasicQuery(new Document() , queryDoc); // 组装query
	query.with(pageable); // 组装分页
	List<Docs> dtos = (List<Docs>) mongoTemplate.find(query, clasz, collectionName); // 映射结果集
	return dtos;
}
 
Example 17
Source File: ProxyResourceDaoImpl.java    From ProxyPool with Apache License 2.0 4 votes vote down vote up
@Override
public List<ResourcePlan> findAllResourcePlan() {
    Query query = new Query();
    query.with(new Sort(Sort.Direction.DESC, "modTime"));
    return mongoTemplate.find(query, ResourcePlan.class, Constant.COL_NAME_RESOURCE_PLAN);
}
 
Example 18
Source File: OrderRepositoryImpl.java    From AppStash with Apache License 2.0 4 votes vote down vote up
@Override
public Order findLastOrder() {
    Query query = query(where("orderDate").exists(true));
    query.with(new Sort(Sort.Direction.DESC, "orderDate"));
    return mongoOperations.findOne(query, Order.class);
}
 
Example 19
Source File: OrderRepositoryImpl.java    From the-app with Apache License 2.0 4 votes vote down vote up
@Override
public Order findFirstOrder() {
    Query query = query(where("orderDate").exists(true));
    query.with(new Sort(Sort.Direction.ASC, "orderDate"));
    return mongoOperations.findOne(query, Order.class);
}
 
Example 20
Source File: PointValueServiceImpl.java    From iot-dc3 with Apache License 2.0 2 votes vote down vote up
/**
 * 排序
 *
 * @param criteriaDefinition CriteriaDefinition
 * @return Query
 */
private Query descQuery(CriteriaDefinition criteriaDefinition) {
    Query query = new Query(criteriaDefinition);
    query.with(Sort.by(Sort.Direction.DESC, "originTime"));
    return query;
}