Reputation: 1807
I have a requirement to do the following operation on a MongoDB table. I need to filter documents based on some column values, I need to group by a column and find the count of values based on the aggregation. Further I want to normalize the count values (i.e., divide the count values of the result by the largest count value of that result).
I have accomplished the first two steps by using the $match and $group parameters of the aggregation pipeline. I'm not sure how to do the normalizing of the results part.
My current query looks something like this
db.list_input_file.aggregate([
{$match:{ 'content.Year' : {$eq : '2006'} }},
{$group:{'_id':'$content.Author', count:{$sum:1}}}
])
Upvotes: 1
Views: 1121
Reputation: 4431
I think it can be done by (with explanation among codes)
db.list_input_file.aggregate([ {
$match : {
'content.Year' : {
$eq : '2006'
}
}
}, {
$group : {
'_id' : '$content.Author',
count : {
$sum : 1
}
}
}, {
$group : {
_id : 0,
maxCount : { // get the largest count value
$max : "$count"
},
docs : { // push all documents into one field to store as an array
$push : "$$ROOT"
}
}
}, {
$project : {
_id : 0,
docs : {
$map : {
"input" : "$docs",
"as" : "e",
"in" : { // retrieve each element
_id : "$$e._id",
count : "$$e.count",
rate : { // add the normalized value here
$divide : [ "$$e.count", "$maxCount"]
}
}
}
}
}
}, {
$unwind : "$docs"
}, {
$project : {
_id : "$docs._id",
count : "$docs.count",
rate : "$docs.rate"
}
} ]);
Upvotes: 3