Any way to create a group by query in mongo? then make it add a column together?

36 views
Skip to first unread message

wozzzzz

unread,
Jun 28, 2014, 9:24:31 AM6/28/14
to mongod...@googlegroups.com
running PHP with mongo and apache on windows 8. I have a database that lists each session a computer has on the internet, it has a MAC address and the number of bytes the computer has downloaded.
the MAC address's of computers appear more than once with the bytes in the other column.
how would I create a grouped query to list all the MAC addresses in the database and add all the bytes column up to get a total for each MAC address?

MAC             BYTES
a3:e4:23      35645
f5:13:d2        1354185
63:a1:c1      8135181
a3:e4:23      67246
f5:13:d2      6848624
a3:e4:23     3542494


then the output when printing the list will look like this with the bytes columns added together and mac's grouped
MAC             BYTES
a3:e4:23      3645385
f5:13:d2        8202809
63:a1:c1      8135181

Asya Kamsky

unread,
Jun 28, 2014, 11:12:54 AM6/28/14
to mongod...@googlegroups.com
Yep, that's what aggregation framework aka pipeline does.  Look up aggregate $group and the $sum function.  The _id should be what you are grouping on.

db.collection.aggregate({$group:{_id:"$a", sums:{$sum:"$counts"}}})

will group by field/column a ($ means contents of a otherwise it would be a literal string) adding all values of field counts for each distinct value of a.

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/b9a7db6c-4acf-4b22-bb54-4456e34440b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

wozzzzz

unread,
Jun 28, 2014, 7:17:59 PM6/28/14
to mongod...@googlegroups.com
using the aggregate function I get "Call to undefined method MongoCollection::aggregate()"
im running driver 1.5.1 in php with apache on windows.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.

Asya Kamsky

unread,
Jun 29, 2014, 2:56:51 PM6/29/14
to mongodb-user
Without seeing your code it would be hard for me to guess where the problem is.
There are a few examples here:  http://www.php.net/manual/en/mongocollection.aggregate.php  Does your code look similar to that?

If so, maybe post it here and someone is likely to spot what the issue is.

Asya



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.

wozzzzz

unread,
Jun 29, 2014, 5:28:38 PM6/29/14
to mongod...@googlegroups.com
tried that with a very simple query to begin with, but aggregate function is undefined. maybe an older version of mongo or something?? where you define the connection mongoclient(), I have to use mongo() as mongoclient doesn't work.
anyway I got it working using arrays and a simple query and added the arrays up.

Asya Kamsky

unread,
Jun 30, 2014, 12:42:23 AM6/30/14
to mongodb-user
Seems unlikely to be old version - 1.5.1 was released in May and aggregate helper has been around for almost two years.

You should be using MongoClient class rather than Mongo - MongoClient has been available since version 1.3.0 - same version that added aggregate, I think - are you sure that you're on 1.5.1?

Asya




Reply all
Reply to author
Forward
0 new messages