Following from earlier lessons where we truncated averages in the $project stage, as I was following along w/ Chapter 4 I decided to try out some calculations on fields in the $bucket stage…
db.companies.aggregate([
{$match: {
founded_year: {$gt: 1980}
}},
{$bucket: {
groupBy: “$number_of_employees”,
boundaries: [ 0, 20, 50, 100, 500, 1000, Infinity],
default: ‘Other’,
output: {
total: {$sum: 1},
average: {$avg: “$number_of_employees”},
avg: {$divide: [{$trunc: {$avg: {$multiply: ["$number_of_employees", 10]}}}, 10]},
categories: {"$addToSet": “$category_code”}
}
}}
]).pretty()
This results in a “$divide is not a unary operation” which I know basically means it can’t be the outside operation on a field in the $group or $bucket stage.
I have to move on to something else so was just curious how I could tweak this code, what I might be missing here? Thanks.