Last active
November 13, 2018 10:27
-
-
Save adrienjoly/ed84d48a728d0d2a76be to your computer and use it in GitHub Desktop.
mongodb "group by" query using aggregation for counting documents per category, on a objectid-timestamp-based subset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db = db.getSiblingDB("whyd_music") // explicitely select collection | |
var jan2014 = ObjectId("52c35a800000000000000000"); // created using http://steveridout.github.io/mongo-object-time/ | |
db.post.aggregate([ | |
{"$sort": {"_id": -1}}, // order: antichronological | |
{"$match": {"_id": {"$gt": jan2014}}}, // only documents that were created after the 1syt january 2014 | |
{"$group": {"_id": "$eId", // group by value of the eId attribute -> _id attribute in the resulting output collection | |
"name": {"$first": "$name"}, // -> include the name of each grouped category | |
"count": {"$sum": 1}}}, // -> count attribute will contain the number of documents for each value of _eid | |
{"$match": {"count": {"$gt": 50}}}, // limit output to results with count > 50 | |
{"$sort": {"count": -1}} // output order: highest count first | |
]).forEach(function(f){print(tojson(f, '', true));}); // avoids mongoshell's paging |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment