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

How to implement Pipeline operator in MongoDB

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

MongoDB in how to achieve the pipeline operator, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

$group

Basic operation

$group can be used to group documents. For example, I want to group orders by city and count the number of orders in each city:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", count: {$sum:1})

We pass the field that we want to group to the _ id field of the $group function, and then add 1 to count whenever one is found, so that we can count the number of orders for each city.

Arithmetic operator

Through the arithmetic operator, we can sum or average the grouped documents. For example, I want to calculate the total order freight for each city, as follows:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", totalFreight: {$sum: "$freight"})

First grouping by address, then summing. Some of the query results are posted here, as follows:

{"_ id": "HaiKou", "totalFreight": 20.0} {"_ id": "HangZhou", "totalFreight": 10.0}

You can also calculate the average freight for each city, as follows:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", avgFreight: {$avg: "$freight"})

Group by address first, and then calculate the average.

Extreme value operator

The extreme operator is used to obtain the edge value of the grouped dataset, such as the most expensive freight for each city, as follows:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", maxFreight: {$max: "$freight"})

Find out the cheapest freight for each city:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", minFreight: {$min: "$freight"})

After grouping by city, get the first freight bill for that city:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", firstFreight: {$first: "$freight"})

Get the last freight bill after grouping:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", lastFreight: {$last: "$freight"}) data operator

$addToSet can put a field after grouping into an array, but the repeated elements will only appear once, and the order in which the elements are added to the array is irregular, such as putting the freight of each city after grouping into an array, as follows:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", freights: {$addToSet: "$freight"})

Duplicate freight will not be added.

$push does not restrict duplicate data and can be added, as follows:

Db.sang_collect.aggregate ({$group: {_ id: "$orderAddressL", freights: {$push: "$freight"}) $unwind

$unwind is used to split the document. You can split the values in the document into separate documents. For example, my data is as follows:

{"_ id": ObjectId ("59f93c8b8523cfae4cf4ba86"), "name": "Lu Xun", "books": [{"name": "Scream", "publisher": "Huacheng Publishing House"}, {"name": "hesitation" "publisher": "published in South China Sea"]}

Use the $unwind command to split it into separate documents, as follows:

Db.sang_books.aggregate ({$unwind: "$books"})

The split results are as follows:

{"_ id": ObjectId ("59f93c8b8523cfae4cf4ba86"), "name": "Lu Xun", "books": {"name": "Scream", "publisher": "Huacheng Publishing House"} {"_ id": ObjectId ("59f93c8b8523cfae4cf4ba86"), "name": "Lu Xun", "books": {"name": "hesitation" "publisher": "published in the South China Sea"}} other operators

The $sort operation can sort documents as follows:

Db.sang_collect.aggregate ({$sort: {orderAddressL:1}})

The usage is the same as in the general search we described earlier, which can be sorted by existing fields or renamed fields, as follows:

Db.sang_collect.aggregate ({$project: {oa: "$orderAddressL"}}, {$sort: {oa:-1}})

1 indicates ascending order, and-1 indicates descending order.

$limit returns the first n documents in the result. The first three documents in the result are represented as follows:

Db.sang_collect.aggregate ({$project: {oa: "$orderAddressL"}}, {$limit:3})

$skip means to skip the first n documents, such as the first five documents, as follows:

Db.sang_collect.aggregate ({$project: {oa: "$orderAddressL"}}, {$skip:5})

The efficiency of $skip is low, so we should use it with caution.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report