mongodb的group简析
mongodb的group简析 数据如下 www.2cto.com { _id : 0, name : hexin0, value : 0, date : ISODate(2012-12-19T11:48:07.151Z), group : 0 } { _id : 1, name : hexin1, value : 1, date : ISODate(2012-12-19T11:48:07.151Z), group : 1 } { _id : 2, name :
mongodb的group简析
数据如下 www.2cto.com
{ "_id" : 0, "name" : "hexin0", "value" : 0, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 1, "name" : "hexin1", "value" : 1, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 2, "name" : "hexin2", "value" : 2, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 3, "name" : "hexin3", "value" : 3, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 4, "name" : "hexin4", "value" : 4, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 5, "name" : "hexin5", "value" : 5, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 6, "name" : "hexin6", "value" : 6, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 7, "name" : "hexin7", "value" : 7, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 8, "name" : "hexin8", "value" : 8, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 9, "name" : "hexin9", "value" : 9, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 10, "name" : "hexin10", "value" : 10, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 11, "name" : "hexin11", "value" : 11, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 12, "name" : "hexin12", "value" : 12, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 13, "name" : "hexin13", "value" : 13, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 14, "name" : "hexin14", "value" : 14, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 15, "name" : "hexin15", "value" : 15, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 16, "name" : "hexin16", "value" : 16, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
{ "_id" : 17, "name" : "hexin17", "value" : 17, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 2 }
{ "_id" : 18, "name" : "hexin18", "value" : 18, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 0 }
{ "_id" : 19, "name" : "hexin19", "value" : 19, "date" : ISODate("2012-12-19T11:48:07.151Z"), "group" : 1 }
需要实现下面sql :
1
select date as d_o_f , goup ,sum(value),count(*),avg(sum(value)/count(*))
2
from xx
3
where name like 'hexin%'
4
group by goup, date
www.2cto.com
1.定义分组的key
1
StringBuilder keyfun = new StringBuilder();
2
keyfun.append("function(d) {");
3
keyfun.append(" return { ");
4
keyfun.append(" goup : d.group ,");
5
keyfun.append(" d_o_f: d.date.getDay() ");
6
keyfun.append(" } ;");
7
keyfun.append(" }");
2. 遍历每个组的处理方式
1
StringBuffer reduce = new StringBuffer();
2
reduce.append("function ( curr, result) {");
3
reduce.append(" result.total += curr.value; ");
4
reduce.append(" result.count++;");
5
reduce.append("}");
3. 计算平均数
1
StringBuffer finalize = new StringBuffer();
2
finalize.append("function(result){");
3
finalize.append(" var weekdays = [ '星期天', '星期一', '星期二',");
4
finalize.append(" '星期三', '星期四', ");
5
finalize.append(" '星期五', '星期六' ];");
6
finalize.append(" result.d_o_f = weekdays[result.d_o_f]; ");
7
finalize.append(" result.avg = Math.round(result.total / result.count); ");
8
finalize.append("}");
4. 调用dao查询
1
Group group = Group.keyFunction(keyfun.toString()).initial("count", 0).initial("total", 0)
2
.reduce(reduce.toString()).finalizeFunction(finalize.toString());
3
//调用自己封装的dao来实现 , 并输出结果
4
List
5
for (Map
6
System.out.println(map);
7
}
封装的查询条件 :
www.2cto.com
1
{ "$regex" : { "$regex" : "^hexin" , "$options" : "m"}}
封装的group命令
01
{
02
"group": {
03
"$keyf": "function(d) { return { goup : d.group , d_o_f: d.date.getDay() } ; }",
04
"$reduce": "function ( curr, result) {result.total += curr.value; result.count++;}",
05
"initial": {
06
"total": 0,
07
"count": 0
08
},
09
"finalize": "function(result){
10
var weekdays = [ '星期天', '星期一', '星期二','星期三', '星期四','星期五', '星期六' ];
11
result.d_o_f = weekdays[result.d_o_f];
12
result.avg = Math.round(result.total / result.count);
13
}",
14
"ns": "c",
15
"cond": {
16
"name": {
17
"$regex": {
18
"$regex": "^hexin",
19
"$options": "m"
20
}
21
}
22
}
23
}
24
}
/////查询结果
{goup=0.0, d_o_f=星期三, total=63.0, count=7.0, avg=9.0}
{goup=1.0, d_o_f=星期三, total=70.0, count=7.0, avg=10.0}
{goup=2.0, d_o_f=星期三, total=57.0, count=6.0, avg=10.0}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512
