verysu 设计模式 设计模式 响应式编程 百度开发平台 codeforces leetcode usfca

导航

mangodb数据库

分页查询实现

public Paging<AsyncQueryHistoricalDataInfoEntity> queryHelpDocList(HelpDocListQuery query) {
try {
Paging<AsyncQueryHistoricalDataInfoEntity> paging = new Paging<>();
if (null != query.getPage() && null != query.getPageSize()) {
paging.setPage(query.getPage());
paging.setPageSize(query.getPageSize());
}
Integer page = query.getPage();
Integer pageSize = query.getPageSize();
List<AggregationOperation> operations = new ArrayList<>();
if (null != query.getNsrsbh()) {
operations.add(Aggregation.match(Criteria.where("nsrsbh").is(query.getNsrsbh())));
}
if (null != query.getNsrdq()) {
operations.add(Aggregation.match(Criteria.where("nsrdq").is(query.getNsrdq())));
}
if (null != query.getSz()) {
operations.add(Aggregation.match(Criteria.where("sz").is(query.getSz())));
}
if (null != query.getSbrq()) {
operations.add(Aggregation.match(Criteria.where("sbrq").is(query.getSbrq())));
}
if (null != query.getSbzt()) {
operations.add(Aggregation.match(Criteria.where("sbzt").is(query.getSbzt())));
}
long totalCount = 0;
//获取满足添加的总页数
if (null != operations&&operations.size()>0) {
Aggregation aggregationCount = Aggregation.newAggregation(operations);
AggregationResults<AsyncQueryHistoricalDataInfoEntity> resultsCount = mongoTemplate.aggregate(aggregationCount, "async_query_historical_data_info", AsyncQueryHistoricalDataInfoEntity.class);
totalCount = resultsCount.getMappedResults().size();
} else {
totalCount = mongoTemplate.count(new Query(),AsyncQueryHistoricalDataInfoEntity.class);
}
operations.add(Aggregation.skip((long) (page - 1) * pageSize));
operations.add(Aggregation.limit(pageSize));
operations.add(Aggregation.lookup("doc_category", "categoryCode", "code", "category"));
operations.add(Aggregation.sort(Sort.Direction.DESC, "solveCount"));
Aggregation aggregation = Aggregation.newAggregation(operations);
AggregationResults<AsyncQueryHistoricalDataInfoEntity> results = mongoTemplate.aggregate(aggregation, "async_query_historical_data_info", AsyncQueryHistoricalDataInfoEntity.class);
paging.setData(results.getMappedResults());
paging.setTotalCount(totalCount);
return paging;
} catch (Exception e) {
throw e;
}
}
使用正则表达式匹配查询

Pattern pattern = Pattern.compile("^.*" + query.getKeyword() + ".*$", Pattern.CASE_INSENSITIVE);
Criteria criteria = Criteria.where("title").regex(pattern);
operations.add(Aggregation.match(criteria));

posted on 2020-07-21 17:08  泳之  阅读(158)  评论(0编辑  收藏  举报

我是谁? 回答错误