当前位置:网站首页>Mongodb usage details_ 4_ Details of common aggregation operations
Mongodb usage details_ 4_ Details of common aggregation operations
2022-06-09 00:51:00 【shstart7】
1. Test data
emp aggregate
db.emp.insert(
[
{
_id: 1,
name: "tom",
age: 13,
likes: ["basketball", "football"],
sex: "F",
dept_id: 1
},
{
_id: 2,
name: "jack",
age: 14,
likes: ["games", "douyin"],
sex: "M",
dept_id: 2
},
{
_id: 3,
name: "amy",
age: 23,
likes: ["code", "basketball"],
sex: "F",
dept_id: 1
},
{
_id: 4,
name: "musk",
age: 42,
likes: ["car", "space"],
sex: "F",
dept_id: 2
},
{
_id: 5,
name: "paul",
age: 34,
likes: [],
sex: "F",
dept_id: 2
}
]
)
dept aggregate
db.dept.insert(
[
{
_id: 1,
d_name: " R & D department "
},
{
_id: 2,
d_name: " Operation and maintenance department "
}
]
)
2. Simple demonstration
A simple aggregation operation is as follows , This aggregation operation will go through two stages of data processing :
- The first pipeline stage is $match: Will filter out all
sex The value is F also dept_id by 2Documents , Then output to the next pipeline operation ; - The second pipeline stage is $project: Used to define the returned field contents , Return here _id name sex dept_id And custom field names emp_name The value is name Value
db.emp.aggregate([
{
$match: {
sex: "F", // matching sex = F Of
dept_id: 2 // matching dept_id = 2 Of
}
},
{
$project: {
// mapping ,1 Show ,0 No display
_id: 1,
name: 1,
sex: 1,
dept_id: 1,
}
}
])
Running results

Java Code
@Test
void agg() {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("sex").is("F").and("dept_id").is(2)),
Aggregation.project("_id", "name", "sex", "dept_id")
);
// Parameter one Aggregation
// Parameter two Aggregate name
// Parameter 3 Corresponding class object
AggregationResults<Emp> emp = mongoTemplate.aggregate(aggregation, "emp", Emp.class);
// call getMappedResults() Method returns the result set
List<Emp> mappedResults = emp.getMappedResults();
}
3.$Match
$match It is mainly used to filter qualified data , Usually we should put $match Put it as far forward as possible , At this time, it will use the index to optimize the query , At the same time, it can also reduce the amount of data that needs to be processed in subsequent stages . Examples are as follows :
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match( Query criteria )
);
4.$Project
$project It is mainly used to define the fields to be returned ,1 The delegate contains this field ,0 Does not include .
// The definition only returns k1 and k2 Value of field , Note that the field name in the set must be filled in here
Aggregation.project("k1", "k2")
5.$group
$group Pipeline phase and in most relational databases group by The function of words and sentences is similar to , They are all used for group computing . Examples are as follows :
db.emp.aggregate(
{
// according to dept_id grouping , Query total age and average age
$group:{
"_id": "$dept_id",
"age_sum": {
$sum: "$age"},
"age_avg": {
$avg: "$age"}
}
}
)
Output

Java Code
@Test
void agg() {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.group("dept_id").sum("age").as("age_sum").avg("age").as("age_avg"),
Aggregation.sort(Sort.by(Sort.Order.asc("_id")))
);
// The third parameter , Specify what type of data each piece of data returns , It is obvious that Map returns .
AggregationResults<Map> emp = mongoTemplate.aggregate(aggregation, "emp", Map.class);
List<Map> mappedResults = emp.getMappedResults();
for (Map mappedResult : mappedResults) {
System.out.println(mappedResult);
}
}
/* * Output results * {_id=1.0, age_sum=36.0, age_avg=18.0} //_id Namely dept_id( Here, according to dept_id grouping ) * {_id=2.0, age_sum=90.0, age_avg=30.0} */
Refer to the more group Aggregate function after

6.$unwind
$unwind Document by Array Split each element of the
Grammar format
{
$unwind:
{
path: <field path>,
includeArrayIndex: <string>,
preserveNullAndEmptyArrays: <boolean>
}
}
- path: Array fields for expansion ;
- includeArrayIndex: Used to display the position information of the corresponding element in the original array ;
- preserveNullAndEmptyArrays: If the field value used for expansion is null Or an empty array , The corresponding document will not be output to the next stage . If you want to output to the next stage, you need to set this attribute to true. The sample statements are as follows :
Example
db.emp.aggregate(
{
$unwind:{
path: "$likes",
includeArrayIndex: "arrayIndex",
preserveNullAndEmptyArrays: true
}
}
)
result 
Be careful : If preserveNullAndEmptyArrays The value of is false Or no Settings , be 5-paul This data will not be output :
Java Code
@Test
void agg() {
Aggregation aggregation = Aggregation.newAggregation(
// Default There is no second parameter , The third parameter is zero false
Aggregation.unwind("likes")
);
AggregationResults<Emp> emp = mongoTemplate.aggregate(aggregation, "emp", Emp.class);
List<Emp> mappedResults = emp.getMappedResults();
for (Emp mappedResult : mappedResults) {
System.out.println(mappedResult);
}
}
/* Emp{id=1, name='tom', age=13, lists=[basketball], sex='F', deptId=1} Emp{id=1, name='tom', age=13, lists=[football], sex='F', deptId=1} Emp{id=2, name='jack', age=14, lists=[games], sex='M', deptId=2} Emp{id=2, name='jack', age=14, lists=[douyin], sex='M', deptId=2} Emp{id=3, name='amy', age=23, lists=[code], sex='F', deptId=1} Emp{id=3, name='amy', age=23, lists=[basketball], sex='F', deptId=1} Emp{id=4, name='musk', age=42, lists=[car], sex='M', deptId=2} Emp{id=4, name='musk', age=42, lists=[space], sex='M', deptId=2} */
7.$sort&skip&limit
$sortMainly used for sorting operations , It should be noted that if you can , This operation should be placed in the first stage of the pipeline as far as possible , Thus, the index can be used to sort , Otherwise, you need to use memory to sort , The sort operation becomes quite expensive , Requires additional memory and computing resources . Examples are as follows$limitLimit the number of returned documents .$skipSkip a certain number of documents .
db.emp.aggregate(
[
{
$sort:{
age:1}}, // Sort by age in ascending order
{
$skip:2}, // Skip the former 2 The number is
{
$limit:2} // Show 2 Data
]
)
Java Code
@Test
void agg() {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.sort(Sort.by(Sort.Order.asc("age"))),
Aggregation.skip(2),
Aggregation.limit(2)
);
AggregationResults<Emp> emp = mongoTemplate.aggregate(aggregation, "emp", Emp.class);
List<Emp> mappedResults = emp.getMappedResults();
for (Emp mappedResult : mappedResults) {
System.out.println(mappedResult);
}
}
/* Running results Emp{id=3, name='amy', age=23, lists=[code, basketball], sex='F', deptId=1} Emp{id=5, name='paul', age=34, lists=[], sex='F', deptId=2} */
8.$lookup
8.1 Relational query
$lookup Similar to most relational databases left outer join , Used to realize the connection between different collections . The basic syntax is as follows :
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
- from: Specify a collection in the same database for connection operations ;
- localField: The fields in the connection set that are used to make the connection ;
- foreignField: Fields in the set to be connected for connection ;
- as: Specify the... For storing matching documents
The name of the new array field. If the specified field already exists , Then cover .
// use dept Primary Key _id Connect emp Table foreign key dept_id
db.dept.aggregate([
{
$lookup:
{
from: "emp",
localField: "_id",
foreignField: "dept_id",
as: "arr"
}
}
])

Java Code
@Test
void agg() {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.lookup("emp", "_id", "dept_id", "arr")
);
AggregationResults<Map> emp = mongoTemplate.aggregate(aggregation, "dept", Map.class);
List<Map> mappedResults = emp.getMappedResults();
for (Map mappedResult : mappedResults) {
System.out.println(mappedResult);
}
}
/* {_id=1.0, d_name= R & D department , arr=[{_id=1.0, name=tom, age=13.0, likes=[basketball, football], sex=F, dept_id=1.0}, {_id=3.0, name=amy, age=23.0, likes=[code, basketball], sex=F, dept_id=1.0}]} {_id=2.0, d_name= Operation and maintenance department , arr=[{_id=2.0, name=jack, age=14.0, likes=[games, douyin], sex=M, dept_id=2.0}, {_id=4.0, name=musk, age=42.0, likes=[car, space], sex=M, dept_id=2.0}, {_id=5.0, name=paul, age=34.0, likes=[], sex=F, dept_id=2.0}]} */
8.2 Non associative query
9. Reference resources
边栏推荐
- svg画的机器人表情包动画代码
- Types et indices d'essais de performance simultanés
- leetcode:剑指 Offer 48. 最长不含重复字符的子字符串【双指针】
- A smiling Emoji, valued at $2billion! This open source project is a bit strong
- Simple implementation of LinkedList underlying source code (common methods)
- ThinkPHP2-RCE漏洞复现
- Apifox: saving every minute of the R & D team
- MySQL tutorial (basic part) Part 05: MySQL data model and SQL language
- Go language goto keyword
- FATFS解读(X):字符串函数
猜你喜欢

Apifox: saving every minute of the R & D team

What is the principle and function of the parental delegation mechanism?

读书笔记-《20世纪最伟大的心理学实验》读后感1
![[tcp/ip] triple handshake process and causes](/img/5c/5dfada817031b4b3ded8b10722b7f7.png)
[tcp/ip] triple handshake process and causes

What is the correct way to start a thread?

怎样才是正确的线程启动方式?

双亲委派机制的原理和作用是什么?
![Leetcode: Sword finger offer 48 Longest substring without repeated characters [double pointer]](/img/dd/0309eaaf43373750a551f226890c19.png)
Leetcode: Sword finger offer 48 Longest substring without repeated characters [double pointer]

R data analysis: how to display statistical results concisely and efficiently

Thanos monitors multiple kubernetes clusters
随机推荐
从刚入测试界到薪资翻倍:聊聊我的测试进阶历程,值得借鉴
Web slider drag selection value slider plug-in
ffmepg. Spec file - the road to dream
Fatfs (X): lire et écrire plusieurs octets (mots)
【饭谈】自研测速平台 or 网上平台二次开发
MySQL tutorial (basic part) Part 05: MySQL data model and SQL language
centernet训练自己的数据集、后处理解析
香港证监会提示NFT风险
Go language basic data type
RedHat 9.0 makes OpenSSH RPM package (9.0p1) -- the road to dream
How to make 100+ orders a day without any supply of free fish? Share the complete strategy!!
ThinkPHP2-RCE漏洞复现
Jz73: flipping word sequences
Svg web animation Halloween night
GO语言基础数据类型
Go language type conversion
What are the categories of IOT network cards
Animation code of robot expression package drawn by svg
Imitating the original theme source code of your sister / imitating the Tiktok mode set of pictures WordPress picture theme template
DevEco Studio的这些预览能力你都知道吗?