mongodb怎么写分页查询语句[mongodb教程]
一、简介
SpringData MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的find的操作,分页查询是返回到匹配文档的游标,可以随意修改查询限制、跳跃、和排序顺序的功能。
我们在查询时find()方法接受Query类型有:
(1)org.springframework.data.mongodb.core.query
(2)org.springframework.data.mongodb.core.query.BasicQuery
Query类提供方法有limit、skip、sort查询限制、跳跃、和排序顺序的功能,BasicQuery继承了Query类。
二、基本分页
Query类提供方法有limit、skip、sort查询限制、跳跃、和排序顺序的功能,我们实现Query查询分页。
第一步:实现分页工具类
package com.qt.robot.mongoEntity;
import java.util.List;
/**
* 分页
* @author 作者:yaohongan
* @create 创建时间:2018年10月9日上午9:53:15
*
*/
public class Page<T> {
// 结果集
private List<T> datas;
// 查询记录数
private int rowCount;
// 每页多少条数据
private int pageSize = 10;
// 第几页
private int pageNo = 1;
// 跳过几条数
private int skip = 0;
/**
* 总页数
* @return
*/
public int getTotalPages() {
return (rowCount + pageSize - 1) / pageSize;
}
public List<T> getDatas() {
return datas;
}
public void setDatas(List<T> datas) {
this.datas = datas;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getSkip() {
skip = (pageNo - 1) * pageSize;
return skip;
}
public void setSkip(int skip) {
this.skip = skip;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
第二步:实现分页
/***
* @author 作者:yaohongan
* @create 创建时间:2018年10月9日下午3:17:03
* @desc 分页根据条件查询机器人id
*/
public Page<VoiceInteractionStatisticsEntity> getVoiceInteractionStatistics(int pageNo, int pageSize,
String robotId, String robotTypeId) {
Page<VoiceInteractionStatisticsEntity> page = new Page<VoiceInteractionStatisticsEntity>();
page.setPageNo(pageNo);
page.setPageSize(pageSize);
Query query = new Query();
Criteria criteria = new Criteria();
int count = 0;
// 查询总数
// 只分页查询
if (StringUtils.isBlank(robotId) && StringUtils.isBlank(robotTypeId)) {
count = (int) mongoTemplate.count(query, collectionName);
page.setRowCount(count);
}
// 根据robotId(机器人id)查询
if (StringUtils.isNoneBlank(robotId)) {
criteria = Criteria.where("robotId").is(robotId);
query = new Query(criteria);
count = (int) mongoTemplate.count(query, collectionName);
page.setRowCount(count);
}
// 根据robotTypeId(机器人设备id)查询
if (StringUtils.isNoneBlank(robotTypeId)) {
criteria = Criteria.where("robotTypeId").is(robotTypeId);
query = new Query(criteria);
count = (int) mongoTemplate.count(query, collectionName);
page.setRowCount(count);
}
// 根据robotId、robotTypeId查询
if (StringUtils.isNoneBlank(robotId) && StringUtils.isNoneBlank(robotTypeId)) {
criteria = Criteria.where("robotTypeId").is(robotTypeId).and("robotId").is(robotId);
query = new Query(criteria);
count = (int) mongoTemplate.count(query, collectionName);
page.setRowCount(count);
}
query.skip(page.getSkip()).limit(page.getPageSize());
List<VoiceInteractionStatisticsEntity> datas = mongoTemplate.find(query,
VoiceInteractionStatisticsEntity.class, collectionName);
page.setDatas(datas);
return page;
}
第三步:应用查询
/***
* @author 作者:yaohongan
* @create 创建时间:2018年10月9日上午10:00:50
* @desc 实时统计语音交互明细
*/
public List<VoiceInteractionStatistics> detailStatistics(int pageNo, int pageSize, String robotId,
String robotTypeId) {
List<VoiceInteractionStatistics> detailStatisticsList = new ArrayList<>();
Date date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Date date1 = new Date();
Calendar calc = Calendar.getInstance();
calc.setTime(date);
calc.add(calc.DATE, -1);
Date minDate = calc.getTime();
Criteria today = null;
Criteria yesterday = null;
Criteria total = null;
Query queryByToday = null;
Query queryByYesterday = null;
Query queryByTotal = null;
// 根据条件查询机器人id
Page<VoiceInteractionStatisticsEntity> page = getVoiceInteractionStatistics(pageNo, pageSize, robotId,
robotTypeId);
if (page != null) {
List<VoiceInteractionStatisticsEntity> list = page.getDatas();
for (VoiceInteractionStatisticsEntity voiceInteractionStatisticsEntity : list) {
// 查询今日消息数
today = Criteria.where("time").gte(date).lt(date1).and("robotId")
.is(voiceInteractionStatisticsEntity.getRobotId());
queryByToday = new Query(today);
int todayTotal = (int) mongoTemplate.count(queryByToday, collectionName);
// 查询昨日消息数
yesterday = Criteria.where("time").gte(minDate).lt(date).and("robotId")
.is(voiceInteractionStatisticsEntity.getRobotId());
queryByYesterday = new Query(yesterday);
int yesterdayTotal = (int) mongoTemplate.count(queryByYesterday, collectionName);
// 查询访问总量
total = Criteria.where("robotId").is(voiceInteractionStatisticsEntity.getRobotId());
queryByTotal = new Query(total);
int totals = (int) mongoTemplate.count(queryByTotal, collectionName);
String robotId1 = voiceInteractionStatisticsEntity.getRobotId();
String robotTypeId1 = voiceInteractionStatisticsEntity.getRobotTypeId();
String robotTypeName = voiceInteractionStatisticsEntity.getRobotTypeName();
VoiceInteractionStatistics voiceInteractionStatistics = new VoiceInteractionStatistics
(robotId1, robotTypeId1, robotTypeName, todayTotal, yesterdayTotal, totals);
detailStatisticsList.add(voiceInteractionStatistics);
}
}
return detailStatisticsList;
}
网,大量的免费MongoDB入门教程,欢迎在线学习!
以上是 mongodb怎么写分页查询语句[mongodb教程] 的全部内容, 来源链接: utcz.com/z/526580.html