monodb 聚合框架 Aggregation
来源:互联网 发布:笔记本thinkpad和mac 编辑:程序博客网 时间:2024/04/29 19:45
前提:本人采用mongodb和spring整合的project
与SQL对照的操作
import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.stereotype.Service;import com.mongodb.AggregationOutput;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBObject;@Servicepublic class NetflowDao {
@Autowired MongoTemplate mongoTemplate;
public static void main(String[] args) { // 假设表中列有 _id name age score Map matchMap = new HashMap(); //where 条件 年龄大于12 matchMap.put("age", new BasicDBObject("$gt", 12)); Map groupMap = new HashMap(); groupMap.put("name","$name"); groupMap.put("age","$age"); BasicDBObject Basicgroup= new BasicDBObject(); //分组条件 Basicgroup.put("_id", groupMap); //计算总分 Basicgroup.put("totalscore", new BasicDBObject("$sum", "$score")); BasicDBObject group=new BasicDBObject("$group", Basicgroup); //分页条件 每次查询返回条数BasicDBObject groupOptlimit = new BasicDBObject();groupOptlimit.put("$limit", 10);//从多少条开始查询BasicDBObject groupOptskip = new BasicDBObject();groupOptskip.put("$skip", 0); BasicDBObject orderbyOpt = new BasicDBObject(); //设置总分排序 正序1 倒序-1 orderbyOpt.append("$sort", new BasicDBObject("totalscore",1)); AggregationOutput aggrResult= mongoTemplate.getCollection("shopnetflowday").aggregate( matchMap,group,orderbyOpt,groupOptskip,groupOptlimit); Iterator<DBObject> iter = aggrResult.results().iterator();while (iter.hasNext()) {System.out.println(iter.next());}}
}
0 0
- monodb 聚合框架 Aggregation
- Aggregation-聚合
- VLAN聚合(VLAN Aggregation)
- mongodb aggregation 聚合
- elasticsearch 之Aggregation聚合
- MongoDB聚合操作Aggregation
- MongoDB Aggregation 聚合管道(Aggregation Pipeline)
- java MongoDB 聚合查询aggregation
- 聚合Aggregation与合成Composition
- 聚合(Aggregation)和组合(Composition)的区别
- 聚合(Aggregation)和组合(Composition)的区别
- 组合与聚合的选用 (Composition & Aggregation)
- 聚合(Aggregation)和组合(Composition)区别
- Composition(组合)与Aggregation(聚合)的区别
- 继承(inheritance),组合(composition),聚合(aggregation)
- MongoDB : Aggregation mongo聚合操作详细说明
- MongoDB 聚合管道(一)(Aggregation Pipeline)
- MongoDB 聚合管道(二)(Aggregation Pipeline)
- 磁盘的磁道(Track)
- Java_并发线程_Condition
- 数据结构之链表
- 【DB.Oracle】解决EXP-00091
- ERROR hive.ql.metadata.Hive - NoSuchObjectException(message:default.hive_born table not found)
- monodb 聚合框架 Aggregation
- 官方文档关于sqlt工具的介绍
- sqlserver存储过程
- 给程序员推荐一些提高工作效率的工具
- poj2479,poj 2593 动归
- 如何学习嵌入式
- 八大排序算法
- cocos2dx3.2学习之路之新建项目
- IOS视频软件开发流程