aggregation framework - Normalizing results from MongoDB -
aggregation framework - Normalizing results from MongoDB -
i have requirement next operation on mongodb table. need filter documents based on column values, need grouping column , find count of values based on aggregation. farther want normalize count values (i.e., split count values of result largest count value of result).
i have accomplished first 2 steps using $match , $group parameters of aggregation pipeline. i'm not sure how normalizing of results part.
my current query looks this
db.list_input_file.aggregate([ {$match:{ 'content.year' : {$eq : '2006'} }}, {$group:{'_id':'$content.author', count:{$sum:1}}} ])
i think can done (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 : { // largest count value $max : "$count" }, docs : { // force documents 1 field store array $push : "$$root" } } }, { $project : { _id : 0, docs : { $map : { "input" : "$docs", "as" : "e", "in" : { // retrieve each element _id : "$$e._id", count : "$$e.count", rate : { // add together normalized value here $divide : [ "$$e.count", "$maxcount"] } } } } } }, { $unwind : "$docs" }, { $project : { _id : "$docs._id", count : "$docs.count", rate : "$docs.rate" } } ]); mongodb aggregation-framework
Comments
Post a Comment