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查询的更多相关文章

  1. distinct group by 去重查询

    select * from dc_restaurants;  31 select DISTINCT (restaurant_name),id from dc_restaurants ; 31 (会按照 ...

  2. count(distinct) 与group by 浅析

    x在传统关系型数据库中,group by与count(distinct)都是很常见的操作.count(distinct colA)就是将colA中所有出现过的不同值取出来,相信只要接触过数据库的同学都 ...

  3. ElasticSearch中"distinct","count"和"group by"的实现

    最近在业务中需要使用ES来进行数据查询,在某些场景下需要对数据进行去重,以及去重后的统计.为了方便大家理解,特意从SQL角度,方便大家能够理解ES查询语句. 1 - distinct ; { &quo ...

  4. MongoDB 之 aggregate $group 巧妙运用

    有这样一组数据: { "campaign_id": "A", "campaign_name": "A", "s ...

  5. 使用子查询可提升 COUNT DISTINCT 速度 50 倍

    注:这些技术是通用的,只不过我们选择使用Postgres的语法.使用独特的pgAdminIII生成解释图形. 很有用,但太慢 Count distinct是SQL分析时的祸根,因此它是我第一篇博客的不 ...

  6. mysql查询不重复的行内容,不重复的记录数.count,distinct

    有这么一个表 记录了id, p_id, p_name , p_content , p_time 1  343        aaa            aaaaaa   2012-09-01 2   ...

  7. PostgreSQL的查询技巧: 零除, GENERATED STORED, COUNT DISTINCT, JOIN和数组LIKE

    零除的处理 用NULLIF(col, 0)可以避免复杂的WHEN...CASE判断, 例如 ROUND(COUNT(view_50.amount_in)::NUMERIC / NULLIF(COUNT ...

  8. 使用GROUP BY统计记录条数 COUNT(*) DISTINCT

    例如这样一个表,我想统计email和passwords都不相同的记录的条数 CREATE TABLE IF NOT EXISTS `test_users` ( `email_id` ) unsigne ...

  9. php查询mysql时,报超出内存错误(select count(distinct))时

    学时服务器查询教练所带人数时,使用select count(distinct(u_STRNO))时报超出内存错误.后参考“mysqld-nt: Out of memory解决方法”http://jin ...

随机推荐

  1. 新手学python-Day2-变量和循环判断

    第二天作业: 初探三级菜单,凭现有知识,注意变量可以不声明,但要提前赋值! 此处shuru = '' 可以不写,因为第7行被赋值了,如果只调用shuru不赋值就会报错 shuru = '' sheng ...

  2. java中的hachcode方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...

  3. UVALive 5412 Street Directions

    Street Directions Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. ...

  4. 主流框架(SSH及SSM)配置文件的模板头文件

    SSH三大框架整合配置头文件模板如下: 一:Spring配置文件(beans.xml)模板:<beans    xmlns="http://www.springframework.or ...

  5. CentOS进入图形界面

    CentOS进入图形界面 学习了: http://www.centoscn.com/CentosBug/osbug/2014/0831/3620.html http://bbs.csdn.net/to ...

  6. VC6.0VB6.0 Scratch等软件

    VC6.0VB6.0 Scratch等软件 http://pan.baidu.com/s/1nv4hJrb

  7. bzoj1830: [AHOI2008]Y型项链(LCP+贪心)

    1830: [AHOI2008]Y型项链 题目:传送门 简要题意: 给出三个字符串,可以对任意字符串进行操作,每次操作都可以再其中一个字符串的末尾删除或添加一个字符,求最小操作数使得所有的字符串相同 ...

  8. zzulioj--1799--wrz的压岁钱(贪心)

     1799: wrz的压岁钱 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 407  Solved: 71 SubmitStatusWeb Boa ...

  9. JSP中动态include与静态include的区别介绍

    转自:https://m.jb51.net/article/43304.htm 动态INCLUDE 用法:<jsp:include page="included.jsp" f ...

  10. 22.boost图模板

    //#pragma warning(disable : 4819) #include <boost/config.hpp> #include <iostream> // for ...