当前位置:网站首页>Mongodb aggregation operation summary
Mongodb aggregation operation summary
2022-07-04 22:37:00 【cui_ yonghua】
1. Role of aggregation operation
MongoDB Middle polymerization (aggregate) It's mainly used to process data ( Such as the statistical average , Make a peace, etc ), And return the calculated data result .
It's kind of similar SQL Statement count(*)
, sum()
, avg()
.
2. aggregate() Method
MongoDB The method of aggregation used in aggregate().
Grammar format :db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Case study : Count the number of articles written by each author , Use aggregate() The calculation results are as follows
db.mycol.aggregate([{
$group : {
_id : "$by_user", num_tutorial : {
$sum : 1}}}])
# similar SQL sentence
select by_user, count(*) from mycol group by by_user;
In the example above , Through fields by_user Fields group data , And calculate by_user The sum of the same values of the fields .
3. Common aggregate expressions
1、$sum Calculate the sum .
db.mycol.aggregate([{
$group : {
_id : "$by_user", num_tutorial : {
$sum : "$likes"}}}])
1、$avg Calculate average
db.mycol.aggregate([{
$group : {
_id : "$by_user", num_tutorial : {
$avg : "$likes"}}}])
1、$min Get the minimum value of all documents in the collection .
db.mycol.aggregate([{
$group : {
_id : "$by_user", num_tutorial : {
$min : "$likes"}}}])
1、$max Get the maximum value of all documents in the collection .
db.mycol.aggregate([{
$group : {
_id : "$by_user", num_tutorial : {
$max : "$likes"}}}])
1、$push Add values to an array , Does not determine whether there are duplicate values .
db.mycol.aggregate([{
$group : {
_id : "$by_user", url : {
$push: "$url"}}}])
1、$addToSet Add values to an array , Will determine whether there are duplicate values , If same value already exists in array , Do not join .
db.mycol.aggregate([{
$group : {
_id : "$by_user", url : {
$addToSet : "$url"}}}])
1、$first Get the first document data according to the sorting of resource documents .
db.mycol.aggregate([{
$group : {
_id : "$by_user", first_url : {
$first : "$url"}}}])
1、$last Get the last document data according to the sorting of resource documents
db.mycol.aggregate([{
$group : {
_id : "$by_user", last_url : {
$last : "$url"}}}])
4. The concept of pipes
Pipes in Unix and Linux It is generally used to take the output of the current command as the parameter of the next command .
MongoDB The polymerization pipeline of will MongoDB The document passes the result to the next pipeline after processing one pipeline . Pipeline operation can be repeated .
expression : Process input documents and output . Expressions are stateless , Only documents that can be used to calculate the current aggregate pipeline , Can't process other documents .
Schematic diagram of polymerization pipeline :
amount to Sql sentence : Select cust_id,sum(amount)as total from orders where status= "A"
Several operations commonly used in aggregation framework :
$project
: Modify the structure of the input document . Can be used to rename 、 Add or remove fields , It can also be used to create calculation results and nested documents .$match
: For filtering data , Output only documents that meet the criteria .$match Use MongoDB Standard query operation .$limit
: Used to restrict MongoDB The number of documents returned by the aggregation pipeline .$skip
: Skip the specified number of documents in the aggregation pipeline , And return the remaining documents .$unwind
: Split an array type field in the document into multiple fields , Each contains a value in the array .$group
: Group documents in a collection , Can be used for statistical results .$sort
: Sort the input documents and output .$geoNear
: Output ordered documents close to a geographic location .
Case study 1:$project
db.article.aggregate({
$project: {
title: 1, author: 1,}});
# By default _id Fields are included , If you want to exclude _id That's how it works
db.article.aggregate({
$project: {
_id : 0 ,title: 1 ,author: 1}});
Case study 2:$match
Case study , Get score greater than 70 Less than or equal to 90 Record , Then send the eligible records to the next stage $group The pipeline operator handles .
db.articles.aggregate([{
$match : {
score : {
$gt : 70, $lte : 90 } } },{
$group: {
_id: null, count: {
$sum: 1 } } }]);
Case study 3: after $skip
After the pipeline operator is processed , The first five documents are " Filter " fall .
db.article.aggregate({
$skip : 5 });
5. SQL Corresponding to aggregation
SQL The term 、 function 、 Concept | MongoDB Aggregation operation |
---|---|
WHERE | $match |
GROUP BY | $group |
HAVING | $match |
SELECT | $project |
ORDER BY | $sort |
LIMIT | $limit |
SUM() | $sum |
COUNT() | $sum |
join | $lookup (3.2 Version added ) |
边栏推荐
- 【烹饪记录】--- 青椒炒千张
- 繁华落尽、物是人非:个人站长该何去何从
- 如何实现轻松管理1500万员工?
- UML图记忆技巧
- idea中pom.xml依赖无法导入
- 醒悟的日子,我是怎么一步一步走向软件测试的道路
- Postgresqlql advanced skills pivot table
- MYSQL架构——用户权限与管理
- The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
- A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
猜你喜欢
醒悟的日子,我是怎么一步一步走向软件测试的道路
Why is Dameng data called the "first share" of domestic databases?
With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
Locust性能测试 —— 环境搭建及使用
Domestic database chaos
MYSQL架构——逻辑架构
Introduction and application of bigfilter global transaction anti duplication component
Concurrent network modular reading notes transfer
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
Convolutional neural network model -- lenet network structure and code implementation
随机推荐
Breakpoint debugging under vs2019 c release
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
AscendEX 上线 Walken (WLKN) - 一款卓越领先的“Walk-to-Earn”游戏
常用的开源无代码测试工具
BigFilter全局交易防重组件的介绍与应用
Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
LOGO特训营 第二节 文字与图形的搭配关系
UML diagram memory skills
LOGO特训营 第五节 字体结构与设计常用技法
Introducing QA into the software development lifecycle is the best practice that engineers should follow
Recommendation of mobile app for making barcode
SQL中MAX与GREATEST的区别
Business is too busy. Is there really no reason to have time for automation?
How diff are the contents of the same configuration item in different environments?
【OpenGL】笔记二十九、抗锯齿(MSAA)
LOGO特訓營 第三節 首字母創意手法
Detailed explanation of flask context
Play with grpc - go deep into concepts and principles
Force buckle_ Palindrome number