Get the App
SLTechnology News&Howtos  ›  Database  › 

Introduction to the method of aggregation

Shulou Source: shulou.com Published: 2022-06-01 22:03:30 10月02日 Update

Aggregation provides various data processing methods for collecting document data and returns the results of the calculation. MongoDB provides three ways to execute aggregation commands: the aggregation pipeline method, the map-reduce method, and the single-target aggregation method.

1. Polymerization pipeline method

The aggregation pipeline method can be directly understood as the aggregate pipeline method, which records a number of numerical documents in the set, and the corresponding values of the keys are classified and counted. This method supports fragment collection operation.

Syntax: db.collection_name.aggregate (

[{$match: {}}]

/ / fieldl is a classified field; field2 is a numeric field with various statistical operators, such as $sum, $avg, $min, $max, $push, $addToSet, $first, $last operator

Db.Sale_detail.aggregate ([{$match: {ok: false} / / search criteria Same as the search criteria used by find (), {$group: {_ id: "$goodsid", total: {$sum: "$amount"} / / Total number of amount fields by goodsid}}])

_ id: "$goodsid", goodsid is the category field name, _ id is a unique field that must be specified and cannot be changed to another name; total is the statistical result field name, which can be any new name that conforms to the naming rules. $sum is the summation operation symbol, and $amount is the summation field, which must be enclosed in double quotes.

2. Map-reduce method

Grammar: db.collection_name. Mapreduce (

Function () {emit (,)}

Function (key, value) {return array.sum (values)}

{query: {}, out:}

Command description:

Function () {emit (,)} returns the corresponding field < field1 > of the collection.

Query: {} query the collection for documents that meet the criteria.

The efficiency of this method is lower than that of aggregation pipeline, and its use is more complex.

3. Single objective aggregation method

Under this method, there are currently two aggregation operation functions: db.collection_name.count () and db.collection_name.distinct ().

(1) Syntax: db.collection_name.count (query, options)

Command description: count the number of documents in the collection that meet the query criteria. Query is the query condition. Option parameters are described in detail as shown in the table.

Statistics on the number of records that meet the criteria

Db.Sale_detail.count ({ok: false})

Starting from Article 2, count the number of records that meet the criteria.

Db.Sale_detail.count ({ok: false}, {skip: 1})

Count the different values of the specified key and return different values

Db.Sale_detail.distinct ("goodsid")

The single-target aggregation method can be used directly after find ().

For example, goodsdb.Sale_detail.find ({ok:false}) .count ()

Goodsdb.Sale_detail.find ({ok:false}). Count (). Skip (1)

Tags: Methods fields statistics conditions documents pipes categories commands methods targets syntax queries different names operators numeric values data quantities results complexity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker Apple macOS OPPO Reno