Get the App
SLTechnology News&Howtos  ›  Database  › 

What are the aggregate commands in MongoDB

Shulou Source: shulou.com Published: 2022-05-31 21:31:07 10月04日 Update

In this issue, the editor will bring you about the aggregation commands in MongoDB. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Polymerization pipeline method:

The pipeline aggregation method can be understood as the aggregate pipeline method, which classifies and statistics the values corresponding to the keys of several numerical documents in the set, which is somewhat similar to group by in SQL language.

The syntax is as follows:

Db.collection.agrregate (

[$match: {}}

{$group: {,}}

]

Description:

Field1 is a classified field; field2 is a numeric field with various statistical operators, such as $sum, $avg, $min,$max, etc.

> use test

> db.test.insert (

... [{id: "001", amount:2,price:15.2,ok:true}

... {id: "001", amount:3,price:14.8,ok:true}

... {id: "002", amount:4,price:40,ok:true}

... {id: "002", amount:2,price:10,ok:true}

... {id: "003", amount:3,price:20.3,ok:true}

...]

.)

BulkWriteResult ({

"writeErrors": []

"writeConcernErrors": []

"nInserted": 5

"nUpserted": 0

"nMatched": 0

"nModified": 0

"nRemoved": 0

"upserted": []

})

> db.test.aggregate ({$match: {ok:true}})

{"_ id": ObjectId ("5b50388dff7043cec86841af"), "id": "001", "amount": 2, "price": 15.2, "ok": true}

{"_ id": ObjectId ("5b50388dff7043cec86841b0"), "id": "001", "amount": 3, "price": 14.8, "ok": true}

{"_ id": ObjectId ("5b50388dff7043cec86841b1"), "id": "002", "amount": 4, "price": 40, "ok": true}

{"_ id": ObjectId ("5b50388dff7043cec86841b2"), "id": "002", "amount": 2, "price": 10, "ok": true}

{"_ id": ObjectId ("5b50388dff7043cec86841b3"), "id": "003", "amount": 3, "price": 20.3, "ok": true}

> db.test.aggregate (

... {

.. $group: {

... _ id:'$id'

... Total: {$sum: "$amount"}

...}

.)

{"_ id": "003", "total": 6}

{"_ id": "002", "total": 12}

{"_ id": "001", "total": 10}

>

Note: _ id:'$id',id is the category field name, total is the statistical result field name, $sum is the summation operation symbol, and $amount is the summation field.

2.map-reduce method:

> var chenfeng=db.test.mapReduce (

... Function () {

... Emit (this.id,this.amount)

...}

... Function (key,values) {

... Return Array.sum (values)

...}

... {query: {ok:true}, out: {replace: "result"}}

.)

> db[ chenfeng.result] .find ()

{"_ id": "001", "value": 5}

{"_ id": "002", "value": 6}

{"_ id": "003", "value": 3}

>

3. Single objective aggregation method:

Syntax:

Db.collection.count (query,options)

For example:

> db.test.distinct ("id")

["001", "002", "003"]

>

> db.test.find ({ok:true}) .count ()

five

These are the aggregate commands in the MongoDB shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Fields categories statistics commands content that is operators numerical values syntax analysis professional small and medium rich content documents articles methods more pipelining goals Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia Microsoft Shulou Information MySQL