Can the new aggregation framework do a $addToSet and $sum ?

868 views
Skip to first unread message

Tom M.

unread,
Jan 27, 2012, 3:23:01 PM1/27/12
to mongodb-dev
Hi,

I love the new aggregation framework so far.
I was curious if it's possible to use $addToSet and then $sum on those
items in the set?
I have a field "language" with values like "en" and "es" and "jp" etc.
I have very easily returned an array of these values in several groups
(by an associated id), but they are just the values that appear and
not the total number of times each one appeared.

Call it users visiting a page, snagging locale from their browser. So
for each page, I want to know how many people came for each locale.
Not just which locales hit the page. The $sum for each.

Is this possible in one call?
Or do I need to run another call to get the total?
I've successfully used $sum but only when using $match to match a
specific language value.
I'd rather not have to run a new command for each possible language
value, while all language values are known in this case, there may be
other cases where the field value can be one of many possible values
that the code couldn't possibly foresee. It's also more taxing to run
that many commands than I'd care for.

Thanks!

Eliot Horowitz

unread,
Jan 28, 2012, 11:03:41 AM1/28/12
to mongo...@googlegroups.com
If I understand, you want to just group by ( language , page ) and
then do a { $sum : 1 }

> --
> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
> To post to this group, send email to mongo...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-dev...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-dev?hl=en.
>

Tom M.

unread,
Feb 13, 2012, 8:13:02 PM2/13/12
to mongodb-dev
Yes, but there's going to be a bunch of fields to aggregate (sub
objects).
If I use $group then do I need to run a new command for each field?
There must be a way to do it all in one shot...??

My documents are more like this:
{
username: 'myname',
email: 'm...@email.com',
location: 'Palo Alto, California',
preferences: {
language: 'en',
timezone: 'PST',
favoriteColor: 'blue'
...
}
}

So say I want to aggregate all users from Palo Alto and get the total
number of english speaking people, favorite colors, etc. All of that.
One call. If possible. I don't know ahead of time (all the time) what
the values for each field may be. When using $addToSet I can get back
all the favorite colors from users that $match the criteria (say
location in this case)...But I can't seem to then get a sum for that.

Thanks for your help Eliot (and anyone else out there).

-Tom


On Jan 28, 8:03 am, Eliot Horowitz <el...@10gen.com> wrote:
> If I understand, you want to just group by ( language , page ) and
> then do a { $sum : 1 }
>
>
>
>
>
>
>
> On Fri, Jan 27, 2012 at 3:23 PM, Tom M. <tom.maiar...@gmail.com> wrote:
> > Hi,
>
> > I love the newaggregationframework so far.

Tom M.

unread,
Feb 13, 2012, 8:27:14 PM2/13/12
to mongodb-dev
Actually, it's like when using $group, $match is ignored.
If $match would work with $group, then I would be seeing results a lot
closer to what I'm after.

Tom M.

unread,
Feb 13, 2012, 8:31:02 PM2/13/12
to mongodb-dev
Ohhhhh I was doing it wrong. I had an array with one element for _id
in the $group.
Making that just a string, $group : { _id :
'$preferences.favoriteColor' } for example fixed it...But again, does
this mean I have to make a new group to get the totals for each field?

Chris Westin

unread,
Mar 26, 2012, 9:37:21 PM3/26/12
to mongo...@googlegroups.com
No, you can have multiple sums after your _id

sum1: {$sum: "$field1"},
sum2: {$sum: "$field2"},....

or other aggregation functions.

Did you get it figured out?

Chris
Reply all
Reply to author
Forward
0 new messages