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 realize the grouping of nested subdocuments in MongoDB

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how to achieve nested sub-document grouping in MongoDB, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

The data of MongoDB nested structure is very common, it achieves an one-to-many relationship by nesting subdocuments. However, the data calculation of subdocuments according to classification and grouping in the nested structure can not be directly realized by the $group aggregation operation. It is necessary to unravel the nested structure and change it from a multi-layer nested structure to a multi-layer single-layer structure. Because of the processing of the intermediate process, and other functions are needed to assist the output. Taking the collection order as an example, the number of term is grouped by category and output in the order from largest to smallest.

[

{category: "movies"

Terms: [{term: "movie 1", total: 1000}, {term: "movie 2", total: 100}]

}

{category: "sports"

Terms: [{term: "football 1", total: 1000}, {term: "tennis 2", total: 120}]

}

{category: "movies"

Terms: [{term: "movie 1", total: 5000}, {term: "movie 2", total: 200}

{term: "movie 3", total: 280}]

}

{category: "sports"

Terms: [{term: "football 1", total: 4000}, {term: "tennis 2", total: 4000}

{term: "tennis 2", total: 450}]

}

]

MongoDB is implemented through the aggregation operation aggregate,group as follows:

Db.order.aggregate ([

{$unwind: "$terms"}

{$group: {_ id: {

Category: "$category"

Term: "$terms.term"}

Total: {$sum: "$terms.total"}

}

}

{$sort: {total:-1}}

{$project: {

_ id: 0

Category: "$_ id.category"

Term: "$_ id.term"

Total:1}}

])

$unwind splits the terms array into multiple strips, then grouped, summed, sorted by $group, and finally filtered the field output with $project.

If there were aggregators to assist MongoDB, there would be no need for such tedious combinatorial operations:

A1=mongo_open ("mongodb://127.0.0.1:27017/raqdb") 2=mongo_shell (A1, "order.find ()). Fetch () 3=A2.conj (terms.derive (A2.category)) 4=A5.group (category,term;~.sum (total): total). Sort (- total) 5 > A1.close () is how to achieve nested subdocument grouping in MongoDB. Xiaobian believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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