MongoDB特殊查询格式化时间(库中时间+8):
db.mogOrderInfo.find({
"itemId":"9140040075",
"orderTime":{"$gte":ISODate("2019-07-09T22:00:00.000Z"),"$lte":ISODate("2019-07-10T04:00:00.000Z")},
"showStatus":{$in:[1,2,5]}},
{"orderId":1,
"orderTime":new Date(1),
"itemId":1}).forEach(
function(a){
a["orderTime"] = (new Date(a["orderTime"]).toString()); printjson(a)
}
)
展示时间为:"orderTime" : "Wed Jul 10 2019 10:08:09 GMT+0800"
如:"orderTime" : "2019-07-10 10:08:09"
.forEach(function(a){
function tran_val(val){
if(parseInt(val)<10){
val="0" +val;
}
return val;
}
var datenew = new Date(a["orderTime"]);
var year=datenew.getFullYear();
var month=tran_val(datenew.getMonth()+1);
var date=tran_val(datenew.getDate());
var hour=tran_val(datenew.getHours());
var minute=tran_val(datenew.getMinutes());
var second=tran_val(datenew.getSeconds());
var datastr=year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
a["orderTime"] = datastr
printjson(a)
}
)
添加排序:.sort({itemId:-1})
一、Criteria中方法含义
1.and:参数拼接 2.is:相当于等于 3.in:相当于sql中的in 4.ne:相当于不等于 5.orOperator:接受多个条件,组成or逻辑 6.where:拼接where条件 7.gte:>= 8.gt:> 9.lte:<= 10.lt:< 11.exists:true存在字段的记录 false:不存在字段的记录 12.模糊查询用正则表达式regex(pattern) 比如:Pattern pattern=Pattern.compile("^.*"+pattern_name+".*$", Pattern.CASE_INSENSITIVE); query.addCriteria(Criteria.where("name").regex(pattern));
二、Update中方法含义
1.set:添加参数
三、聚合函数
1.match进行条件拼接
2.group 分组字段(如果为表中不存在属性,不分组全部统计,如果为表中存在属性,按属性分组统计)
其中group下属性 1:count 统计分组各条件统计数量 2:sum 分组各条件指定属性求和 3:addToSet 分组各条件指定属性中某属性进行收集为集合 4.max 分组各条件指定属性求最大一个值 注意:使用各属性必须设定别名 as
3.sort 添加排序属性,注意:排序字段必须为group统计后面显式某个属性
4.skip 和 limit 如同 mysql中limit a,b
四、代码实例
1.保存
this.mongoTemplate.save(实体)
2.删除
Criteria criteria = Criteria.where("_id").is(id);
if (null != criteria) {
Query query = new Query(criteria);
if (null != query && this.findOne(query) != null) {
mongoTemplate.remove(query, this.getEntityClass());
}}
3.更新(如果存在属性更新值,如果不存在属性则新增属性和值)
Query query = Query.query(Criteria.where("_id").is(mogOrderInfo.getId()));
Update update = this.orderParamUpdate(条件实体);
this.updateMulti(query,update);
4.查询多条件列表
Query query = new Query();
query.addCriteria(this.orderParamFile(条件实体));
Sort sort = new Sort(Sort.Direction.DESC, "orderTime");
query.with(sort);
List
5.查询多条件列表分页
BasePageVo page = reqOrderVo.getBasePageVo();
Query query = new Query();
query.addCriteria(this.orderParamFile(条件实体));
Sort sort = new Sort(Sort.Direction.DESC, "orderTime");
query.with(sort);
List
long count = this.count(query);
Pagination
pagination.setList(list);
6.统计聚合函数查询
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(this.orderParamFile(条件实体)),
Aggregation.group("id").count().as("countOrderNum").sum("priceTotal").as("sumPriceTotal").sum("awardMoney").as("sumAwardMoney").sum("commMoney").as("sumCommMoney"));
AggregationResults
List
重要:Aggregation.group("id")中 如果 id随意字段(不属数据库表实体字段) 表示不分组 如果id为数据库表实体字段就是按照字段分组
返回结果集:自定义VitalOrderVo
public class VitalOrderVo implements Serializable {
private Long parentUserId;//下单人直属上级id
private Long countOrderNum;//订单总数
private Long sumPriceTotal;//订单实付总额 单位:分
private Long sumAwardMoney;//提奖总额 单位:分
private Long sumCommMoney;//佣金总额 单位:分
}
7.统计聚合函数查询 分页分组
BasePageVo page = reqOrderVo.getBasePageVo();
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(this.orderParamFileParentUserId(条件实体)),
Aggregation.group("parentUserId").count().as("countOrderNum").sum("priceTotal").as("sumPriceTotal").sum("awardMoney").as("sumAwardMoney").sum("commMoney").as("sumCommMoney"),
Aggregation.sort(new Sort(Sort.Direction.DESC, "sumAwardMoney")),
Aggregation.skip(page.getStartRow()),
Aggregation.limit(page.getPageSize()));
AggregationResults
//获取数量
Aggregation aggregationCount = Aggregation.newAggregation(
Aggregation.match(this.orderParamFileParentUserId(条件实体)),
Aggregation.group("parentUserId"));
AggregationResults
Pagination
pagination.setList(aggResult.getMappedResults());
7.统计聚合函数查询 分页分组(封装多属性二次查询)
BasePageVo page = reqOrderVo.getBasePageVo();
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(this.orderParamFileParentUserId(条件实体)),
Aggregation.group("orderId").addToSet("skuId").as("skuIds").max("orderTime").as("orderTime"),
Aggregation.sort(new Sort(Sort.Direction.DESC, "orderTime")),
Aggregation.skip(page.getStartRow()),
Aggregation.limit(page.getPageSize()));
AggregationResults
//获取数量
Aggregation aggregationCount = Aggregation.newAggregation(
Aggregation.match(this.orderParamFileParentUserId(条件实体)),
Aggregation.group("orderId"));
AggregationResults
Pagination
pagination.setList(vitalOrderNoPays);
*** 多条件过滤和条件拼接方法如下:
private Criteria orderParamFile(ReqOrderVo reqOrderVo){
Criteria criteria = new Criteria();
String skuId = reqOrderVo.getSkuId();
if(StringUtils.isNotBlank(skuId)){
criteria.and("skuId").is(skuId);
}
Long parentOutUserId = reqOrderVo.getParentOutUserId();
if(parentOutUserId!=null){
criteria.and("parentUserId").ne(parentOutUserId);
}
List
if(showStatusList!=null){
criteria.and("showStatus").in(showStatusList);
}
String orderIdUserWeixin = reqOrderVo.getOrderIdUserWeixin();
if(StringUtils.isNotBlank(orderIdUserWeixin)){
criteria.orOperator(Criteria.where("orderId").is(orderIdUserWeixin),Criteria.where("userWeixin").is(orderIdUserWeixin));
}
Date orderStartTime = reqOrderVo.getOrderStartTime();
Date orderEndTime = reqOrderVo.getOrderEndTime();
if(orderStartTime!=null&&orderEndTime!=null){
criteria.and("orderTime").gte(orderStartTime).lte(orderEndTime);
}
return criteria;
}
private Update orderParamUpdate(MogOrderInfo orderInfo){
Update update = new Update();
Long orderId = orderInfo.getOrderId();
if(orderId!=null){
update.set("orderId", orderId);
}
String deliveryId = orderInfo.getDeliveryId();
if(StringUtils.isNotBlank(deliveryId)){
update.set("deliveryId", deliveryId);
}
List
if(userIdRelation!=null&&userIdRelation.size()>0){
update.set("userIdRelation", userIdRelation);
}
return update;
}
*** 分页工具类和方法如下:
public class BasePageVo implements Serializable {
private Integer pageNo = 1;
private Integer startRow = 0;
private Integer pageSize = 10;
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
if (pageNo < 1) {
this.pageNo = 1;
} else {
this.pageNo = pageNo;
}
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getStartRow() {
this.startRow = (pageNo-1)*this.pageSize;
return startRow;
}
}
import java.util.ArrayList;
import java.util.List;
public class Pagination
public Pagination() {
}
/**
* 构造器
* @param pageNo 页码
* @param pageSize 每页几条数据
* @param totalCount 总共几条数据
*/
public Pagination(int pageNo, int pageSize, int totalCount) {
super(pageNo, pageSize, totalCount);
}
/**
* 构造器
* @param pageNo 页码
* @param pageSize 每页几条数据
* @param totalCount 总共几条数据
* @param list 分页内容
*/
public Pagination(int pageNo, int pageSize, int totalCount, List
super(pageNo, pageSize, totalCount);
this.list = list;
}
/**
* 第一条数据位置
* @return
*/
public int getFirstResult() {
return (pageNo - 1) * pageSize;
}
/**
* 当前页的数据
*/
private List
/**
* 当前页的分页样式
*/
private List
/**
* 获得分页内容
* @return
*/
public List
return list;
}
/**
* 设置分页内容
* @param list
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setList(List list) {
this.list = list;
}
/**
* 获得分页样式
* @return
*/
public List
return pageView;
}
/**
* 设置分页样式
*
* @param list
*/
public void setPageView(List
this.pageView = pageView;
}
/**
* 分页显示样示部分
*/
public void pageView(String url,String params){
pageView = new ArrayList
if(this.pageNo != 1){
pageView.add("首页");
pageView.add("上一页");
}else{
pageView.add("首页");
pageView.add("上一页");
}
if(this.getTotalPage() <= 10){
for (int i = 0; i < this.getTotalPage(); i++) {
if((i+1)==this.pageNo){
pageView.add(""+this.pageNo+"");
i = i+1;
if(this.pageNo==this.getTotalPage())break;
}
pageView.add(""+(i+1)+"");
}
}else if(this.getTotalPage() <= 20){
//没有把...加上
int l = 0;
int r = 0;
if(this.pageNo<5){
l=this.pageNo-1;
r=10-l-1;
}else if(this.getTotalPage()-this.pageNo<5){
r=this.getTotalPage()-this.pageNo;
l=10-1-r;
}else{
l=4;
r=5;
}
int tmp = this.pageNo-l;
for (int i = tmp; i < tmp+10; i++) {
if(i==this.pageNo){
pageView.add(""+this.pageNo+"");
i = i+1;
if(this.pageNo==this.getTotalPage()) break;
}
pageView.add(""+(i)+"");
}
}else if(this.pageNo<7){
for (int i = 0; i < 8; i++) {
if(i+1==this.pageNo){
pageView.add(""+this.pageNo+"");
i = i+1;
}
pageView.add(""+(i+1)+"");
}
pageView.add("...");
pageView.add(""+(this.getTotalPage()-1)+"");
pageView.add(""+(this.getTotalPage())+"");
}else if(this.pageNo>this.getTotalPage()-6){
pageView.add(""+(1)+"");
pageView.add(""+(2)+"");
pageView.add("...");
for (int i = this.getTotalPage()-8; i
pageView.add(""+this.pageNo+"");
i = i+1;
if(this.pageNo==this.getTotalPage()) break;
}
pageView.add(""+(i+1)+"");
}
}else{
pageView.add(""+(1)+"");
pageView.add(""+(2)+"");
pageView.add("...");
pageView.add(""+(this.pageNo-2)+"");
pageView.add(""+(this.pageNo-1)+"");
pageView.add(""+this.pageNo+"");
pageView.add(""+(this.pageNo+1)+"");
pageView.add(""+(this.pageNo+2)+"");
pageView.add("...");
pageView.add(""+(this.getTotalPage()-1)+"");
pageView.add(""+(this.getTotalPage())+"");
}
if(this.pageNo != this.getTotalPage()){
pageView.add("下一页");
pageView.add("尾页");
} else{
pageView.add("下一页");
pageView.add("尾页");
}
pageView.add("共" + getTotalPage() + "页 到第页 ");
}
}
public class SimplePage implements java.io.Serializable,Paginable {
private static final long serialVersionUID = 1L;
public static final int DEF_COUNT = 20;
/**
* 检查页码 checkPageNo
* @param pageNo
* @return if pageNo==null or pageNo<1 then return 1 else return pageNo
*/
public static int cpn(Integer pageNo) {
return (pageNo == null || pageNo < 1) ? 1 : pageNo;
}
public SimplePage() {
}
/**
* 构造器
* @param pageNo 页码
* @param pageSize 每页几条数据
* @param totalCount 总共几条数据
*/
public SimplePage(int pageNo, int pageSize, int totalCount) {
setTotalCount(totalCount);
setPageSize(pageSize);
setPageNo(pageNo);
adjustPageNo();
}
/**
* 调整页码,使不超过最大页数
*/
public void adjustPageNo() {
if (pageNo == 1) {
return;
}
int tp = getTotalPage();
if (pageNo > tp) {
pageNo = tp;
}
}
/**
* 获得页码
*/
public int getPageNo() {
return pageNo;
}
/**
* 每页几条数据
*/
public int getPageSize() {
return pageSize;
}
/**
* 总共几条数据
*/
public int getTotalCount() {
return totalCount;
}
/**
* 总共几页
*/
public int getTotalPage() {
int totalPage = totalCount / pageSize;
if (totalPage == 0 || totalCount % pageSize != 0) {
totalPage++;
}
return totalPage;
}
public int getStartRow() {
this.startRow = (pageNo-1)*this.pageSize;
return startRow;
}
/**
* 是否第一页
*/
public boolean isFirstPage() {
return pageNo <= 1;
}
/**
* 是否最后一页
*/
public boolean isLastPage() {
return pageNo >= getTotalPage();
}
/**
* 下一页页码
*/
public int getNextPage() {
if (isLastPage()) {
return pageNo;
} else {
return pageNo + 1;
}
}
/**
* 上一页页码
*/
public int getPrePage() {
if (isFirstPage()) {
return pageNo;
} else {
return pageNo - 1;
}
}
protected int totalCount = 0;
protected int pageSize = 20;
protected int pageNo = 1;
protected int startRow=0;
/**
* if totalCount<0 then totalCount=0
*
* @param totalCount
*/
public void setTotalCount(int totalCount) {
if (totalCount < 0) {
this.totalCount = 0;
} else {
this.totalCount = totalCount;
}
}
/**
* if pageSize< 1 then pageSize=DEF_COUNT
* @param pageSize
*/
public void setPageSize(int pageSize) {
if (pageSize < 1) {
this.pageSize = DEF_COUNT;
} else {
this.pageSize = pageSize;
}
}
/**
* if pageNo < 1 then pageNo=1
* @param pageNo
*/
public void setPageNo(int pageNo) {
if (pageNo < 1) {
this.pageNo = 1;
} else {
this.pageNo = pageNo;
}
}
}
public interface Paginable {
//总记录数
public int getTotalCount();
//总页数
public int getTotalPage();
//每页记录数
public int getPageSize();
//当前页号
public int getPageNo();
//是否第一页
public boolean isFirstPage();
//是否最后一页
public boolean isLastPage();
//返回下页的页号
public int getNextPage();
//返回上页的页号
public int getPrePage();
}