[DOCS-880] add use case 'distinct' to agg sql chart Created: 12/Dec/12  Updated: 14/Mar/14  Resolved: 12/Dec/12

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: v1.0

Type: Task Priority: Major - P3
Reporter: Kay Kim (Inactive) Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 9 years, 48 weeks, 6 days ago

 Description   

db.final.aggregate(
{ $group: { _id:

{ a: "$a", b: "$b" }

} },
{ $group: {_id: null, count:

{ $sum: 1 }

} } )



 Comments   
Comment by Kay Kim (Inactive) [ 14/Mar/14 ]

Hi Luke –
Sorry I had misread your SQL statement. I'm glad you found a solution for you.

To request new features, you can open a SERVER ticket rather than a DOCs (documentation ticket).

Comment by Luke Thompson [ 14/Mar/14 ]

That distinct('executor').length doesn't do what I'm asking in the SELECT GROUP - it just tells me how many distinct items there are, not the count of each distinct item.

...

I was looking at writing that myself, but I found someone already did it!

This was what I was after:
http://skratchdot.com/projects/mesh/
https://github.com/skratchdot/mongodb-distinct2/blob/master/distinct2.js

It would be really nice to embed that into the real shell instead of having to add it to my environment by hand. Would it be possible to raise that?

Comment by Kay Kim (Inactive) [ 12/Mar/14 ]

Hi Luke –
we have something similar on the SQL to Aggregation page http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/

instead of the the SQL to MongoDB page. Sorry for the confusion.

Also, in the mongo shell, we do have a distinct() method (http://docs.mongodb.org/manual/reference/method/db.collection.distinct) which returns an array of distinct values, so you could also do:

db.task.distinct( 'executor').length

Regards

Comment by Luke Thompson [ 11/Mar/14 ]

Was this example removed? I was looking for it here: http://docs.mongodb.org/master/reference/sql-comparison/

I was looking for a better way of doing this:
SELECT executor, count(executor) FROM task GROUP BY executor

Which I'm always implementing like this:
db.task.aggregate({$group: {'_id':'$executor', count: {$sum:1}}})

Ideally the shell would support this:
db.task.countDistinct('executor')

Comment by auto [ 12/Dec/12 ]

Author:

{u'date': u'2012-12-12T21:56:03Z', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: merge: DOCS-880
Branch: master
https://github.com/mongodb/docs/commit/d82210e4f31d969a7b67e89e80c76776a5980bd8

Comment by auto [ 12/Dec/12 ]

Author:

{u'date': u'2012-12-12T17:44:42Z', u'email': u'kay.kim@10gen.com', u'name': u'kay'}

Message: DOCS-880 add count of distinct groupings
Branch: master
https://github.com/mongodb/docs/commit/7089c0f3abc7c1d33f4809f8e0ce72e4319acc04

Generated at Thu Feb 08 07:39:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.