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 绘图与可视化 seaborn

    Seaborn是一个基于matplotlib的Python数据可视化库.它提供了一个高级界面,用于绘制有吸引力且信息丰富的统计图形. 主页:http://seaborn.pydata.org/ 官方教 ...

  2. VUE:class与style强制绑定

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. IE6 浏览器常见兼容问题 共23个

    1.<!DOCTYPE HTML>文档类型的声明. 产生条件:IE6浏览器,当我们没有书写这个文档声明的时候,会触发IE6浏览器的怪异解析现象: 解决办法:书写文档声明. 2.不同浏览器当 ...

  4. 封装HttpClient进行http请求与https请求

    一.https忽略证书 /** * 用于进行Https请求的HttpClient * * @author joey * */ public class SSLClient { public stati ...

  5. php安装redis扩展 windows

    官方php_redis.dll 找了很久,感谢热心的网友,这是php官方的 php_redis.dll http://windows.php.net/downloads/pecl/releases/r ...

  6. Java基础学习总结(59)——30 个java编程技巧

    1.return 一个空的集合,而不是 null 如果一个程序返回一个没有任何值的集合,请确保一个空集合返回,而不是空元素.这样你就不用去写一大堆 "if else" 判断null ...

  7. CSVHelper读出乱码 解决方案

    using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) using (StreamRe ...

  8. Action访问ServletAPI的三种方式

    一.前言 Struts是一种基于MVC设计模式的web应用框架,主要担任C的角色,用于分离页面显示和业务逻辑处理,那其实在我们学习jsp的时候学过一个具有类似功能的东西——servlet.其实Stru ...

  9. java.util.UnknownFormatConversionException: Conversion = ''';

    今天在测试一个新的项目,在执行sql查询报表的时候.由于我的sql中带有%,导致在输出日志时报错“java.util.UnknownFormatConversionException: Convers ...

  10. extjs动态导入

    Ext.Loader.setConfig({enabled: true}); Ext.Loader.setPath("util", "../wx/jsUtil" ...