Having migrated to Amazon DocumentDB, I suppose I am in in dire straits as it does not support $bucket
stage operator, as mentioned in Amazon DocumentDB - Supported MongoDB APIs, Operations, and Data Types.
– Data –
This is raw data, one can insert it in MongoDB :
[{
"_id": {
"$oid": "6447544a4e512379dee1ced4"
},
"name": "Mike Trout",
"uniformNumber": 27,
"team": "Los Angeles Angels",
"date": "2023/02",
"totalHomeruns": 333
},{
"_id": {
"$oid": "644755b54e512379dee1ced5"
},
"name": "Shohei Ohtani",
"uniformNumber": 17,
"team": "Los Angeles Angels",
"date": "2023/03",
"totalHomeruns": 337
},{
"_id": {
"$oid": "644755b54e512379dee1ced6"
},
"name": "Aaron Judge",
"uniformNumber": 99,
"team": "New York Yankees",
"date": "2023/01",
"totalHomeruns": 295
},{
"_id": {
"$oid": "644755b54e512379dee1ced7"
},
"name": "Mookie Betts",
"uniformNumber": 11,
"team": "Los Angeles Dodgers",
"date": "2023/01",
"totalHomeruns": 251
},{
"_id": {
"$oid": "644755b54e512379dee1ced8"
},
"name": "Bryce Harper",
"uniformNumber": 25,
"team": "Philadelphia Phillies",
"date": "2023/02",
"totalHomeruns": 217
},{
"_id": {
"$oid": "644755b54e512379dee1ced9"
},
"name": "Ronald Acuna Jr.",
"uniformNumber": 23,
"team": "Atlanta Braves",
"date": "2023/02",
"totalHomeruns": 229
},{
"_id": {
"$oid": "644755b54e512379dee1cee1"
},
"name": "Marcus Semien",
"uniformNumber": 19,
"team": "Texas Rangers",
"date": "2023/03",
"totalHomeruns": 196
},{
"_id": {
"$oid": "644755b54e512379dee1cee2"
},
"name": "Vladimir Guerrero Jr.",
"uniformNumber": 6,
"team": "Toronto Blue Jays",
"date": "2023/03",
"totalHomeruns": 188
},{
"_id": {
"$oid": "644755b54e512379dee1cee3"
},
"name": "Yordan Alvarez",
"uniformNumber": 10,
"team": "Houston Astros",
"date": "2023/02",
"totalHomeruns": 137
},{
"_id": {
"$oid": "644755b54e512379dee1cee4"
},
"name": "Andrew Benintendi",
"uniformNumber": 23,
"team": "Chicago Whitesox",
"date": "2023/02",
"totalHomeruns": 116
},{
"_id": {
"$oid": "644755b54e512379dee1cee5"
},
"name": "Wander Franco",
"uniformNumber": 33,
"team": "Tampa Bay Rays",
"date": "2023/02",
"totalHomeruns": 97
}]
– Aggregation –
The $bucket
stage looks like this :
{
groupBy: '$totalHomeruns',
boundaries: [
0, 95, 105,115,125,135,145,155,165,175,185,195,
205,215,225,235,245,255,265,275,285,295,
305,315,325,335,345,355,365,375,385,395,405,
],
default: 'Other',
output: {
count: {
$sum: 1,
},
playerData: {
$push: {
name: '$name',
uniformNumber: '$uniformNumber',
team: '$team',
totalHomeruns: '$totalHomeruns',
date: '$date'
},
},
},
}
– Expected Result –
This is what $bucket
stage above produces :
[
{
_id: 95,
count: 1,
playerData: [
{
name: 'Wander Franco',
uniformNumber: 33,
team: 'Tampa Bay Rays',
totalHomeruns: 97,
date: '2023/02',
},
],
},
{
_id: 115,
count: 1,
playerData: [
{
name: 'Andrew Benintendi',
uniformNumber: 23,
team: 'Chicago Whitesox',
totalHomeruns: 116,
date: '2023/02',
},
],
},
...
{
_id: 335,
count: 1,
playerData: [
{
name: 'Shohei Ohtani',
uniformNumber: 17,
team: 'Los Angeles Angels',
totalHomeruns: 337,
date: '2023/03',
},
],
}
]
– Question –
Is it possible to get the expected output above without $bucket
stage operator?
Thank you in advance!