Please share what you tried so far. This way we avoid losing time working on a solution you already know it is not working. This could also save us by simply adapting or improving on a partially working solution.
Please share the sample result you would like.
The above requirement is not clear enough to propose anything meaningful.
//Bucket Stock Data ; But it defaults them into “OTHER” as overall SUM INVENTORY DATA
db.artists.aggregate( [
// First Stage
{
$bucket: {
groupBy: “$Stock_Count”, // Field to group by
boundaries: [ 10, 25, 35, 45, 55, 80, 100 ], // Boundaries for the buckets
default: “Other”, // Bucket ID for documents which do not fall into a bucket
output: { // Output for each bucket
“count”: { $sum: 1 },
“artists” :
{
$push: {
“Product_ID”: $Product_ID ,
“Store_ID”: $Store_ID ,
“Stock_Count”: “$Stock_Count”
}
}
}
}
}
I was able to find the solution by myself - thanks to MongoDB University videos & Stackoverflow
Sharing the working code below for anyone coming across similar scenario to achieve it.
//Getting Products and its stores + inventory count using nested group scenario
[
{
$group:
/**
* _id: The id of the group.
* fieldN: The first field name.
*/
{
_id: {
Product_ID: "$Product_ID",
Store_ID: "$Store_ID",
Stock_Count: "$Stock_Count",
},
},
},
{
$group:
/**
* _id: The id of the group.
* fieldN: The first field name.
*/
{
_id: "$_id.Product_ID",
Subset: {
$push: {
Store_ID: "$_id.Store_ID",
Stock_Count:
"$_id.Stock_Count",
},
},
},
},
]