当前位置:网站首页>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 2 Documents , 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

 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here

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
 Insert picture description here

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

  • $sort Mainly 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
  • $limit Limit the number of returned documents .
  • $skip Skip 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"
        }
   }
]) 

 Insert picture description here

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

Reference resources

9. Reference resources

MongoDB polymerization

SpringBoot operation MongoDB Aggregation operation

原网站

版权声明
本文为[shstart7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/159/202206081954375087.html