├── .gitignore ├── group-sum.js ├── joins.js ├── unwind.js ├── basics.js ├── sort-skip-limit.js └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /group-sum.js: -------------------------------------------------------------------------------- 1 | db.tweets.aggregate([ 2 | { 3 | $group: { 4 | _id: "$user.screen_name", 5 | tweetCount: { 6 | $sum: 1 7 | }, 8 | favoriteCount: { 9 | $sum: "$favorite_count" 10 | } 11 | } 12 | }, 13 | { 14 | $sort: { 15 | tweetCount: 1 16 | } 17 | } 18 | ]) 19 | -------------------------------------------------------------------------------- /joins.js: -------------------------------------------------------------------------------- 1 | db.product.aggregate([ 2 | { 3 | $lookup: { 4 | from: "category", 5 | localField: "categoryId", 6 | foreignField: "_id", 7 | as: "categoryDetail" 8 | } 9 | }, 10 | { 11 | $project: { 12 | "name": "$name", 13 | "category": { 14 | $arrayElemAt: ["$categoryDetail", 0] 15 | } 16 | } 17 | } 18 | ]) 19 | -------------------------------------------------------------------------------- /unwind.js: -------------------------------------------------------------------------------- 1 | db.tweets.aggregate([ 2 | { 3 | $project: { 4 | 'username': '$user.screen_name', 5 | 'hashtags': '$entities.hashtags', 6 | 'tweet': '$text' 7 | } 8 | }, 9 | { 10 | $unwind: '$hashtags' 11 | }, 12 | { 13 | $group: { 14 | _id: '$hashtags.text', 15 | count: { 16 | $sum: 1 17 | } 18 | } 19 | }, 20 | { 21 | $sort: { 22 | count: -1 23 | } 24 | }, 25 | { 26 | $limit: 5 27 | } 28 | ]); 29 | -------------------------------------------------------------------------------- /basics.js: -------------------------------------------------------------------------------- 1 | db.tweets.aggregate([ 2 | // Stage 1: 3 | // Accepts the query, similar to `find` 4 | { 5 | $match: { 6 | 'user.verified': true 7 | } 8 | }, 9 | // Stage 2: 10 | // Extract the username and create dummy description 11 | { 12 | $project: { 13 | // Here we are not renaming the field and getting it as it is 14 | // "user.screen_name": 1, 15 | 'user_name': '$user.screen_name', 16 | 'description': { 17 | '$concat': ['Find them @', '$user.screen_name'] 18 | } 19 | } 20 | } 21 | ]); -------------------------------------------------------------------------------- /sort-skip-limit.js: -------------------------------------------------------------------------------- 1 | db.tweets.aggregate([ 2 | // Stage 1: 3 | // Get all the tweets which are not retweets from the verified users 4 | { 5 | $match: { 6 | "user.verified": true, 7 | "retweeted_status": null 8 | } 9 | }, 10 | // Stage 2: 11 | // Sort them by the retweet count in each 12 | { 13 | $sort: { 14 | "retweet_count": -1 15 | } 16 | }, 17 | // Stage 3, 4: 18 | // Skip the top retweet and get the second most retweeted one 19 | { 20 | $skip: 1 21 | }, 22 | { 23 | $limit: 1 24 | } 25 | // Stage 5: 26 | // Project to get only the relevant fields 27 | { 28 | $project: { 29 | "tweet": "$text", 30 | "username": "$user.screen_name", 31 | "retweet_count": -1, 32 | } 33 | } 34 | ]); 35 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## egghead-mongo-aggregation 2 | Codebase supporting my "Aggregation in MongoDB" lectures on egghead 3 | 4 | ## Videos 5 | * [Introduction and Basic Pipelines – `$match`, `$project`, `$concat`](https://egghead.io/lessons/egghead-find-and-manipulate-data-using-aggregation-in-mongodb) 6 | * [Sort, Skip and Limit records in MongoDB aggregation – `$sort`, `$skip`, `$skip`](https://egghead.io/lessons/egghead-sort-skip-and-limit-records-in-mongodb-aggregation) 7 | * [Writing Join Queries — `$lookup`, `$project`, `$arrayElemAt`](https://egghead.io/lessons/egghead-write-joins-in-mongodb) 8 | * [Group by and Sum in MongoDB — `$group`, `$sum`, `$sort` operators](https://egghead.io/lessons/egghead-group-by-and-sum-in-mongodb) 9 | * [Perform Group Operations on Array fields - `$unwind`, `$group`, `$sort`, `$limit`](https://egghead.io/lessons/egghead-perform-group-operations-on-array-fields-in-mongodb), 10 | * ... 11 | * [Suggest!](https://github.com/kamranahmedse/egghead-mongo-aggregation/issues/new) 12 | --------------------------------------------------------------------------------