MongoDB count distinct group by JavaAPI查询
import java.net.UnknownHostException;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
public class MongoDBTest {
private static DB db = null; static{
Mongo mongo = null;
try {
mongo = new Mongo("localhost", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
}
db = mongo.getDB("TestDB"); //连接数据
} /**
* select count(*) from list
*/
public Long m1() {
DBCollection collection = db.getCollection("list");
return collection.count();
} /**
* select count(*) from list where userId = 'orange'
*/
public Long m2(){
DBCollection listCollection = db.getCollection("list");
BasicDBObject query =new BasicDBObject();
query.put("userId", "orange");
return listCollection.count(query);
} /**
* select count(*) from list where userId = 'orange' and time >= '20101201' and time <= '20101211'
*/
public Long m3(){
DBCollection listCollection = db.getCollection("list");
BasicDBObject query =new BasicDBObject();
query.put("userId","orange");
query.put("time", new BasicDBObject("$gte", "20101201").append("$lte", "20101211"));
return listCollection.count(query);
} /**
* select count(distinct(userId)) from detail where time >= '20101201' and time <='20101211'
*/
public int m4() {
DBCollection collection = db.getCollection("detail");
BasicDBObject query = new BasicDBObject();
query.put("time", new BasicDBObject("$gte", "20101201").append("$lte", "20101211"));
return collection.distinct("userId", query).size();
} /**
* select date_format(time, '%Y-%m-%d %H') as sj ,count(*) from detail group by sj
*/
public void m5() {
DBCollection collection = db.getCollection("detail");
String formatDate = "function(obj,doc){"
+ "var date = new Date(doc.time);"
+ "var dateKey = date.getFullYear()+\"-\"+(date.getMonth()+1)+\"-\"+date.getDate(); "
+ "return {'time':datekey}" + "}";
BasicDBObject key = new BasicDBObject();
key.put(formatDate, true); // 要分组的列
BasicDBObject query = new BasicDBObject(); // where条件
String reduce = "function (obj, prev) {prev.count++}";
BasicDBObject initial = new BasicDBObject();
initial.append("count", 0); // 每列初始值
BasicDBList group = (BasicDBList) collection.group(key, query, initial, reduce);
System.out.println(group);
} }
MongoDB count distinct group by JavaAPI查询的更多相关文章
- distinct group by 去重查询
select * from dc_restaurants; 31 select DISTINCT (restaurant_name),id from dc_restaurants ; 31 (会按照 ...
- count(distinct) 与group by 浅析
x在传统关系型数据库中,group by与count(distinct)都是很常见的操作.count(distinct colA)就是将colA中所有出现过的不同值取出来,相信只要接触过数据库的同学都 ...
- ElasticSearch中"distinct","count"和"group by"的实现
最近在业务中需要使用ES来进行数据查询,在某些场景下需要对数据进行去重,以及去重后的统计.为了方便大家理解,特意从SQL角度,方便大家能够理解ES查询语句. 1 - distinct ; { &quo ...
- MongoDB 之 aggregate $group 巧妙运用
有这样一组数据: { "campaign_id": "A", "campaign_name": "A", "s ...
- 使用子查询可提升 COUNT DISTINCT 速度 50 倍
注:这些技术是通用的,只不过我们选择使用Postgres的语法.使用独特的pgAdminIII生成解释图形. 很有用,但太慢 Count distinct是SQL分析时的祸根,因此它是我第一篇博客的不 ...
- mysql查询不重复的行内容,不重复的记录数.count,distinct
有这么一个表 记录了id, p_id, p_name , p_content , p_time 1 343 aaa aaaaaa 2012-09-01 2 ...
- PostgreSQL的查询技巧: 零除, GENERATED STORED, COUNT DISTINCT, JOIN和数组LIKE
零除的处理 用NULLIF(col, 0)可以避免复杂的WHEN...CASE判断, 例如 ROUND(COUNT(view_50.amount_in)::NUMERIC / NULLIF(COUNT ...
- 使用GROUP BY统计记录条数 COUNT(*) DISTINCT
例如这样一个表,我想统计email和passwords都不相同的记录的条数 CREATE TABLE IF NOT EXISTS `test_users` ( `email_id` ) unsigne ...
- php查询mysql时,报超出内存错误(select count(distinct))时
学时服务器查询教练所带人数时,使用select count(distinct(u_STRNO))时报超出内存错误.后参考“mysqld-nt: Out of memory解决方法”http://jin ...
随机推荐
- 修改Myeclies作者用户名
首先点击 windos 点击 preferences 依次点击左侧 Java -> Code Style -> Code Templates 击右侧Comments,将其中的Types项, ...
- 使用xpath进行熟悉href属性
HTML文档 <html> <body> <a href="http://www.example.com">Example</a> ...
- ajax异步请求获取数据,实现滚动数字的效果。
BackgroundPositionAnimate.js下载 需要导入的js: <script type="text/javascript" src="js/jqu ...
- Git学习总结(9)——如何构建你自己的 Git 服务器
现在我们将开始学习如何构建一个Git服务器,如何在具体的事件中写一个针对特定的触发操作的自定义Git(例如通告),如何发布你的代码到一个网站. 目前为止,用户对Git的焦点主要在Git的使用上.这篇文 ...
- NYIST 1107 最高的奖励
最高的奖励 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 请问:挖掘机技术哪家强?AC了告诉你! 给你N(N<=3*10^4)个任务,每个任务有一个截止完成时 ...
- 每天学点Python之comprehensions
每天学点Python之comprehensions 推导式能够简化对数据的处理,让代码简洁的同一时候还具有非常高的可读性.这在Python中非经常见. 列表推导式 通过列表推导式能够对列表中的全部元素 ...
- 安卓获取百度地图的Api key
1.进入开发中心 2.如何获取SHA1 3.如何获取包名
- 几个常用ORACLE运维监控的SQL语句
1.消耗CPUSELECT a.CPU_TIME, --CPU时间 百万分之一(微秒) a.OPTIMIZER_MODE,--优化方式 a.EXEC ...
- xBIM 基础15 IFC导出Excel报表
系列目录 [已更新最新开发文章,点击查看详细] IFC导出Excel空间报表文件 本篇将向您展示从IFC文件读取数据所需的一些概念.它使用IFC4接口,适用于IFC2x3和IFC4型号.要创建 ...
- HD-ACM算法专攻系列(7)——Text Reverse
问题描述: 源码: /**/ #include"iostream" #include"string" using namespace std; void Pri ...