How to Group and Summarize MongoDB Subdocuments
As the most popular non-relational database, MongoDB achieves the one-to-many relationship through the nested subdocument structure. Grouping the nested subdocuments during application development is not uncommon. Below is a MongoDB collection:
{ |
We are trying to group records by items.goodsNsId and sum items.count. Here is the desired result:
{ |
If we use a MongoDB script to do this, we need to group and sum the sub-records under each record and then join the intermediate results with the source collection. It is complicated.
It is convenient to get it done with esProc SPL. We group and sum subdocuments under each record and assign the aggregate result to the subdocuments.
1. Write SPL script warehouse.dfx in esProc:
A |
B |
|
1 |
=mongo_open("mongodb://localhost:27017/cata") |
/ Connect to mongodb |
2 |
=mongo_shell(A1,"warehouse.find(,{_id:0})").fetch() |
/ Get data from the warehouse collection |
3 |
>A2.run(items=(items.groups(goodsNsId; price, sum(count):count))) |
/ Group and sum the subdocuments and assign the aggregate results to subdocuments |
4 |
>A1.close() |
/ Close database connection |
2. Start debugging and execute the code. Below is the value of cell A2:
A2 |
warehouseNsId |
brandId |
financeOwnerId |
amount |
items |
10 |
37.0 |
1.23188280881790566E18 |
0.0 |
[[1353, 256.00, 3....]] |
|
15 |
35.0 |
1.23188280881790566E18 |
0.0 |
[[1327, 238.00, 17...]] |
|
… |
… |
… |
3. Execute the script and return result as follows:
A2 |
warehouseNsId |
brandId |
financeOwnerId |
amount |
items |
10 |
37.0 |
1.23188280881790566E18 |
0.0 |
[1353, 1799] |
|
15 |
35.0 |
1.23188280881790566E18 |
0.0 |
[1327, 1539] |
|
… |
… |
… |
Click the items field value in the first row of the above table sequence, and we can get the following table:
goodsNsId |
price |
count |
1353 |
256.00 |
13.0 |
1799 |
254.80 |
6.0 |
A3: run() function loops through each record to group and sum the subdocuments and assign the aggregate result to the sub-records.
Read How to Call an SPL Script in Java to learn about the integration of an SPL script with a Java program.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL