Aggregation - Percentage

3,119 views
Skip to first unread message

Andrés

unread,
Sep 22, 2015, 3:53:02 AM9/22/15
to mongodb-user
Hello,

I have a collection with following documents:

{idnumber: "5324", colour: "red"}
{idnumber: "9435", colour: "blue"}
{idnumber: "4323", colour: "yellow"}
{idnumber: "3123", colour: "red"}
{idnumber: "2220", colour: "blue"}
{idnumber: "6456", colour: "blue"}

I am looking to get percentage of colours, something like this:

blue 50,000000%
red 33,333333%
yellow 16,666667%

Any suggestions?

Thank you!


Andrés

unread,
Sep 22, 2015, 5:40:50 AM9/22/15
to mongodb-user

I have managed to calculate percentages, but I still have a step missisng ...


I need to calculate the total number of documents (in yellow) inside the [], inside the "aggregation" operation.

 

[

        { "$group": { "_id": {"colour":  "$colour"}, "count": { "$sum": 1 }}},   

        { "$project": {

            "count": 1,

            "percentage": {

                "$concat": [ { "$substr": [ { "$multiply": [ { "$divide": [ "$count", 6 ] }, 100 ] }, 0,4 ] }, "", "%" ]}

            }

        }

    ]


That is what is supposed to do, but I can not do it. because where I am working, I can only type "aggregation" operations:


var total = db.mycollection.count ()

 

[

        { "$group": { "_id": {"colour":  "$colour"}, "count": { "$sum": 1 }}},   

        { "$project": {

            "count": 1,

            "percentage": {

                "$concat": [ { "$substr": [ { "$multiply": [ { "$divide": [ "$count", total ] }, 100 ] }, 0,4 ] }, "", "%" ]}

            }

        }

    ]


So, I would like to know whether I can calculate the TOTAL inside the []


Thanks!

Asya Kamsky

unread,
Sep 29, 2015, 1:47:01 AM9/29/15
to mongodb-user
You can calculate the total in aggregation using $group on _id null but you would need to preserve all the color details and then unwind them to calculate percentages.  It would probably be simpler to do it the way you are doing it now.

I'm not sure what you mean when you say you can only type aggregation - why can't you do a simple count on the collection first?

Asya


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/f8a2d991-b672-4e01-8631-e45ba52b22f7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers

Pankaj Saraswat

unread,
Sep 29, 2015, 11:28:59 PM9/29/15
to mongodb-user
How about this?

db.pankaj.aggregate([
    {$group:{
        "_id":"",
        "total":{$sum:1},
        "color set":{$push:"$color"}}},
    {$unwind:"$color set"},
    {$group:{
        "_id":{"color":"$color set","total":"$total"},

        "count":{$sum:1}}},
    {$project:{
        "_id":{"color":"$_id.color"},
        "weight":{$multiply:[{$divide:["$count","$_id.total"]},100]}}}
    ])
Reply all
Reply to author
Forward
0 new messages