Reports from a collection

248 views
Skip to first unread message

J.M.

unread,
Apr 27, 2015, 6:13:18 AM4/27/15
to suppor...@runmyprocess.com
Hi,

I'm trying to show some stats based on the info I have in my collection. For this, I've been following this tutorial:

http://docs.runmyprocess.com/Developer_Guide/Web_Interface/Design/Charts/Chart_Dashboard_Collection

but I need to do something that I don't if it's possible on MongoDB.

I want to make two counts of two distict fields( something like
SELECT Count(x), Count(y) FROM T1 GROUP BY date; more or less)

I've been reading aggreation functions on MongoDB doc, but I don't know how ca i make this. For counting, only one field, I've made this:

var pipelines =
[
{
"$group": {
"_id": {
"fecha":"$fecha"
},
"count":{
"$sum":1
}
}
},
{
"$project":{
"DATE":"$_id.fecha",
"TOTAL":"$count"
}
}
];

...and it works perfectly. But I now need to count not only the number of documents for each date, I need to count the number of places, and I had thougth something like this:

var my_pipelines =
[
{
"$group":
{
"_id": {
"fecha":"$fecha",
"ticket_id": "$ticket_id",
},
"count_tickets":{
"$sum":1
},
}
},
{
"$group":
{
"_id": {
"fecha":"$fecha",
"location": "$location"
},
"count_places":{
"$sum":1
}
}
},
{
"$project":
{
"DATE":"$_id.fecha",
"TOTAL_TICKETS":"$count_tickets",
"TOTAL_PLACES":"$count_places"
}
}
];

But It seems that, two groups clauses are not allowed.
How can I count two distinct fields?
I would thank some help, please.

Regards

Pray Desai

unread,
May 6, 2015, 3:07:50 PM5/6/15
to suppor...@runmyprocess.com, javiermm...@gmail.com

Hi JM,

You can try grouping two distinct fields with code like below but you cannot have distinct ids for distinct group fields :

var my_pipelines = [{
                    "$group" : {
                        "_id" : {
                        "State" : "$state"
                        },
"Total_Cars": {
                            "$sum" : "$nb_of_cars"
                        },
                        "Total_Motors" : {
                            "$sum" : "$nb_of_motors"
                        }
                    }
                    }, {
                        "$project" : {
                        "state" : "$_id.State",
                        "TOTAL_CARS" : "$Total_Cars",
                        "TOTAL_MOTORS" : "$Total_Motors"
                        }
                    }
                    ];


Regards,
Pray Desai,
Fujitsu RunMyProcess.
Reply all
Reply to author
Forward
0 new messages