Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the method of aggregation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report