8. Aggregation Framework
Declarative (BSON, not JavaScript)
Implemented in C++
Flexible and functional
Operation pipeline
Computational expressions
Plays nice with sharding
10. Pipeline
Process a stream of documents
Original input is a collection
Final output is a result document
Series of operators
Filter or transform data
Input/output chain
p x|ge ogd|ha n1
sa rpmno ed
11. Pipeline Operators
$ac
mth
$rjc
poet
$ru
gop
$nid
uwn
$ot
sr
$ii
lmt
$kp
si
19. $group
Group documents by an ID
Field path reference
Object with multiple references
Constant value
Other output fields are computed
$a,$i,$v,$u
mx mn ag sm
adoe, ps
$ d T S t$ u h
$is,$at
frt ls
Processes all data in memory
23. $unwind
Operate on an array field
Yield documents for each array value
Nothing for absent or empty fields
Error for non-array fields
Complements $ r u
gop
24. $unwind
Yielding Multiple Documents from One
{ ► {$nid $ujcs
uwn:"sbet"}
tte TeGetGtb"
il:"h ra asy,
IB:"715109"
SN 9887513,
sbet:[
ujcs ▼
"ogIln"
Ln sad,
"e ok,
NwYr" {
"90"
12s tte TeGetGtb"
il:"h ra asy,
]
IB:"715109"
SN 9887513,
} sbet:"ogIln"
ujcs Ln sad
}
{
tte TeGetGtb"
il:"h ra asy,
IB:"715109"
SN 9887513,
sbet:"e ok
ujcs NwYr"
}
{
tte TeGetGtb"
il:"h ra asy,
IB:"715109"
SN 9887513,
sbet:"90"
ujcs 12s
}
25. $sort
Sort documents by one or more fields
Uses familiar cursor format
Waits for earlier pipeline operator to return
In-memory unless early and indexed
26. $sort
Sort All Documents in the Pipeline
{tte TeGetGtb"}
il:"h ra asy ► {$ot il:1}
sr:{tte }
{tte BaeNwWrd
il:"rv e ol"}
▼
{tte TeGae fWah
il:"h rpso rt"} {tte Aia am
il:"nmlFr"}
{tte Aia am
il:"nmlFr"} {tte BaeNwWrd
il:"rv e ol"}
{tte Lr fteFis
il:"odo h le"} {tte Fhehi 5"}
il:"arnet41
{tte FtesadSn"}
il:"ahr n os {tte FtesadSn"}
il:"ahr n os
{tte IvsbeMn
il:"niil a"} {tte IvsbeMn
il:"niil a"}
{tte Fhehi 5"}
il:"arnet41 {tte Lr fteFis
il:"odo h le"}
{tte TeGae fWah
il:"h rpso rt"}
{tte TeGetGtb"}
il:"h ra asy
27. $limit
Limit Documents through the Pipeline
{tte TeGetGtb"}
il:"h ra asy ► {$ii:5}
lmt
{tte BaeNwWrd
il:"rv e ol"}
▼
{tte TeGae fWah
il:"h rpso rt"} {tte TeGetGtb"}
il:"h ra asy
{tte Aia am
il:"nmlFr"} {tte BaeNwWrd
il:"rv e ol"}
{tte Lr fteFis
il:"odo h le"} {tte TeGae fWah
il:"h rpso rt"}
{tte FtesadSn"}
il:"ahr n os {tte Aia am
il:"nmlFr"}
{tte IvsbeMn
il:"niil a"} {tte Lr fteFis
il:"odo h le"}
{tte Fhehi 5"}
il:"arnet41
28. $skip
Skip Over Documents in the Pipeline
{tte TeGetGtb"}
il:"h ra asy ► {$kp
si:5}
{tte BaeNwWrd
il:"rv e ol"}
▼
{tte TeGae fWah
il:"h rpso rt"} {tte FtesadSn"}
il:"ahr n os
{tte Aia am
il:"nmlFr"} {tte IvsbeMn
il:"niil a"}
{tte Lr fteFis
il:"odo h le"} {tte Fhehi 5"}
il:"arnet41
{tte FtesadSn"}
il:"ahr n os
{tte IvsbeMn
il:"niil a"}
{tte Fhehi 5"}
il:"arnet41
29. Extending the Framework
Adding new pipeline operators, expressions
$ u and $ e for output control
ot te
https://jira.mongodb.org/browse/SERVER-3253
32. Boolean Operators
Input array of one or more values
$n,$r
ad o
Short-circuit logic
Inverse values with $ ont
BSON standard for converting non-booleans
n l , u d f n dzero values → f l e
ul neie, as
Non-zero values, strings, dates, objects → t u
re
34. Arithmetic Operators
Input array of one or more numbers
$d,$utpy
ad mlil
Input array of two operands
sbrc, dvd, md
$ u t a t$ i i e$ o
35. String Operators
$ t c s c pcase-insensitive comparison
sraem
$ m is case-sensitive
cp
$ o o e and $ o p e case change
tLwr tUpr
$ u s rfor sub-string extraction
sbt
Not encoding aware
Assumes Latin alphabet
36. Date Operators
Extract values from date objects
$ a O Y a , $ a O M n h$ a O W e
dyfer dyfot, dyfek
$ e r$ o t , $ e k
ya, mnh we
hu, mnt, scn
$ o r$ i u e$ e o d
47. Sharding
Split the pipeline at first $ r u or $ o t
gop sr
Shards execute pipeline before that point
mongos merges results and continues
Early $ a c may excuse shards
mth
CPU and memory implications for mongos