How to do distinct using aggregation framework

1,319 views
Skip to first unread message

songhe yang

unread,
Feb 11, 2012, 7:38:37 AM2/11/12
to mongodb-user
RT

I didn't find the distinct operator in aggregation framework in
pipeline operators, so how to do distinct using aggregation framework?

Scott Hernandez

unread,
Feb 11, 2012, 9:25:32 AM2/11/12
to mongod...@googlegroups.com
You use $group. Are you just trying to get the number or the list of
distinct values?

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

Scott Hernandez

unread,
Feb 11, 2012, 9:30:09 AM2/11/12
to mongod...@googlegroups.com
You use $group. Are you just trying to get the number or the list of
distinct values?

On Sat, Feb 11, 2012 at 7:38 AM, songhe yang <songh...@gmail.com> wrote:

songhe yang

unread,
Feb 12, 2012, 12:48:26 AM2/12/12
to mongodb-user
I want to get the list of distinct values.

One more question:

Why not provide the distinct operator in pipeline operator?

Scott Hernandez

unread,
Feb 12, 2012, 12:58:30 AM2/12/12
to mongod...@googlegroups.com
It is easy to do like so:

db.runCommand({
"aggregate" : "people",
"pipeline" : [
{
"$group" : {
"_id" : "$name"
}
}]})

This creates a unique list of the name field.

Not sure distinct makes sense as a pipeline operator. Feel free to add
a request: http://jira.mongodb.org/

SreenathV

unread,
May 8, 2014, 9:15:39 PM5/8/14
to mongod...@googlegroups.com
Distinct can be achieved using pipes in Aggregate framework...Sample code snippet athttp://www.techiesinfo.com/code-snippet analysing clickstream
Reply all
Reply to author
Forward
0 new messages