Aggregation Lab Slides
Aggregation Lab Slides
● No stupid questions
● Actively discuss
● Be respectful
Agenda
● MongoDB Query API
● Aggregation Pipeline Framework
● Filtering data
● Grouping data
● Modifying data
MongoDB Query API
MongoDB Query API: rich MongoDB
and expressive {
customer_id : 1,
first_name : "Mark",
last_name : "Smith",
● Find anyone with phone # “1-212…”
Expressive
● Check if the person with number “555…” is on the “do not city : "San Francisco",
queries call” list
phones: [
● Find the best offer for the customer at geo coordinates of {
Geospatial 42nd St. and 6th Ave number : "1-212-777-1212",
type : "work"
Text search ● Find all tweets that mention the firm within the last 2 days
},
● Count and sort number of customers by city, compute {
Aggregation min, max, and average spend
number : "1-212-777-1213",
● Add an additional phone number to Mark Smith’s record type : "cell"
Native binary without rewriting the document }
JSON support ● Update just 2 phone numbers out of 10
● Sort on the modified date ]
}
JOIN ● Query for all San Francisco residences, lookup their
($lookup) transactions, and sum the amount by person
Rich Expressions
db.orders.aggregate( [
$match stage {$match: { status: "A" } },
$group stage {$group: { _id: "$cust_id", total: { $sum: "$amount" } } }
] )
Aggregation features
A feature rich framework for data transformation and Analytics
Aggregation Pipeline Builder Export your finished pipeline code to your language
of choice to execute in application code
MongoDB Compass
Aggregation
Stages
● Filters the data, similar to .find()
● Can use various operators such
as $gt, $lt, $and, $or
db.books.aggregate([
{
$match: {
pages: 100
}
$match ]);
}
● Returns a subset of the fields
db.books.aggregate([
{
$project: {title:1, cover: 1}
$project ]);
}
● Limits the number of returned
documents
db.books.aggregate([
{
$limit: 5
$limit ]);
}
● Match the exact array
db.collection.find({
tags: ['red', 'blank']
});
db.books.aggregate([
{
$count: "fieldName"
$count ]);
}
● Group documents together
db.books.aggregate([
{$group:{
_id: "$year",
totalPages: {$sum: "$pages"}
}}
$group ])
● Joins documents across
collections
● Because you can do joins doesn’t
meet you should use them
db.authors.aggregate([
{$lookup: {
from: "books",
localField: "books",
foreignField: "_id",
as: "booksWritten"
}
$lookup ])
At the end of this exercise, you
will be able to:
● Run aggregation pipelines with the $count stage
db.books.aggregate([
{$set: {
readingTimeHours:
{$divide: [{$multiply: ["$pages", 2]},
60]}}},
$set ])
● Exports the data into another
database and/or collection
{ $out: {
db: "<output-db>",
coll: "<output-collection>"
}}
$out
At the end of this exercise, you
will be able to:
● Run an aggregation pipeline that modifies the data
https://mdb.link/developer-day-toronto