I'm currently utilizing the Mongo aggregate framework and have a collection structured like this:
[
{
_id: 123,
name: "john",
age: 30,
fruit: "apple",
},
{
_id: 345,
name: "moore",
age: 45,
fruit: "mango",
},
{
_id: 545,
name: "carl",
age: 30,
fruit: "grape",
},
{
_id: 96,
name: "shelby",
age: 25,
fruit: "apple",
},
{
_id: 86,
name: "loris",
age: 48,
fruit: "mango",
},
{
_id: 76,
name: "carl",
age: 55,
fruit: "grape"
}
]
My goal is to query and create a pipeline that returns the count of specific fruits falling under certain $bucket boundaries. The desired result should look like this...
[
{
"_id": Specific_Boundary,
"userCount": Number_Of_Users_Falling_Under,
"fruitsLie": [
{fruit_names_of_users_in_this_boundary : fruit_counts},
]
},
{
"_id": 0,
"userCount": 3,
"fruitsLie": [
{apple: 2},
{grape: 1}
]
},
{
"_id": 40,
"userCount": 2,
"fruitsLie": [
{mango: 2}
]
},
{
"_id": "more than 50",
"userCount": 1,
"fruitsLie": [
{grape: 1}
]
}
]
For example, under the age of 30 we have 3 users - 2 eat apples and 1 eats grapes, so the fruitsLie
field performs these calculations.
What are the various approaches available to solve this problem with specific $bucket boundaries? Please provide a detailed explanation for each stage as I am new to aggregates and still learning...