Skip to content

Instantly share code, notes, and snippets.

View zchking's full-sized avatar
💭
I may be slow to respond.

Bill Zhang zchking

💭
I may be slow to respond.
View GitHub Profile
@zchking
zchking / mongodb_distinct_count.js
Last active August 5, 2020 02:50 — forked from clarkenheim/mongodb_distinct_count.js
MongoDB equivalent of an SQL query to get the distinct values of a field in a collection including the count of documents which have each distinct value (distinct with count)
//equivalent of MySQL SELECT COUNT(*) AS cnt, fieldName FROM someTable GROUP BY fieldName;
db.someCollection.aggregate([{"$group" : {_id:"$fieldName", cnt:{$sum:1}}}]);
//as above but ordered by the count descending
//eg: SELECT COUNT(*) AS cnt, fieldName FROM someTable GROUP BY fieldName ORDER BY cnt DESC;
db.someCollection.aggregate([{"$group" : {_id:"$fieldName", cnt:{$sum:1}}}, {$sort:{'cnt':-1}}]);
db.order.aggregate([{ $group: { _id: "$seller_email", total_order: { "$sum": 1 },
seller_address_1 : { $first: '$seller_address_1' },
seller_country : { $first: '$seller_country' },
@zchking
zchking / README.md
Created July 16, 2019 10:03 — forked from hernamesbarbara/README.md
A numpy implementation ~5× faster than using itertools.