We’ve setup a basic application which writes events to a mongo instance. Here’s a sample write:
"_id" : ObjectId("50fee761472870a3d610956e"),
"user_id" : "pa-XXXXXXXXX",
"event_id" : 1,
"date_created" : 1337798856,
"url" : "http://news.yahoo.com/australian-tycoon-worlds-richest-woman-103810206.html"
What we’d like to do is retrieve results grouped by URL and a count of certain event_id’s. Like event_grouping_a might consist of event_ids 1,6,35 and event_grouping_b might consist of 2,66,103. A sample output would look something like this:
{
url: "http://news.yahoo.com/australian-tycoon-worlds-richest-woman-103810206.html",
event_grouping_a: 46,
event_grouping_b: 34
},
{
url: "http://news.yahoo.com/another-cool-story",
event_grouping_a: 105,
event_grouping_b: 59
}
Any idea on how to perform this type of aggregation/grouping? The end goal is to have it in PHP, but I’ve messed with doing it in the mongod console to no avail. I can get it to group by URL, but I can’t get it do display both event types under a single parent URL. It spits out something like this:
{
url: "http://news.yahoo.com/australian-tycoon-worlds-richest-woman-103810206.html",
event_grouping_a: 46
event_grouping_b: 0
},
{
url: "http://news.yahoo.com/australian-tycoon-worlds-richest-woman-103810206.html",
event_grouping_a: 0
event_grouping_b: 34
},
...
The above two should be merged into 1, but I can’t for the life of me figure it out … any suggestions?
I don’t necessarily like the
$orstatements to make this work, but this query should work for you from php if you are using the 1.3+ mongo driver:—