Advanced MongoDB Aggregation Pipelines
-
Upload
tom-schreiber -
Category
Data & Analytics
-
view
184 -
download
1
Transcript of Advanced MongoDB Aggregation Pipelines
![Page 1: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/1.jpg)
Advanced MongoDB Aggregation Pipelines
Tom Schreiber
![Page 2: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/2.jpg)
{ "About me" : {
"Name" : "Tom Schreiber",
"Title" : "Senior Consulting Engineer",
"Email" : "[email protected]",
"Twitter" : "@SchreiberTom1",
"Location" : "London, UK"
}
}
![Page 3: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/3.jpg)
![Page 4: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/4.jpg)
![Page 5: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/5.jpg)
"Data should be easy to work with."
"The data should serve you. You should not serve the data."
"The same (data-) model in your head, in your code, and in your data - NO impedance mismatch." Eliot Horowitz
CTO & Co-Founder of MongoDB MongoDB World’16
![Page 6: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/6.jpg)
MongoDB Aggregation Framework
"Data should be easy to work with."
"The data should serve you. You should not serve the data."
"The same (data-) model in your head, in your code, and in your data - NO impedance mismatch."
![Page 7: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/7.jpg)
A Highly Composable Compute Pipeline
MongoDB Aggregation Framework
$sum$match
![Page 8: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/8.jpg)
A Highly Composable Compute Pipeline
34
3 13
646
36
4
MongoDB Aggregation Framework
$sum$match
![Page 9: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/9.jpg)
A Highly Composable Compute Pipeline
34
3 13
646
36
4
MongoDB Aggregation Framework
$sum$match
![Page 10: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/10.jpg)
A Highly Composable Compute Pipeline
34
3 13
646
36
4
MongoDB Aggregation Framework
$sum$match
Notions of computation and monads
Eugenio Moggi∗
Abstract
The λ-calculus is considered an useful mathematical tool in the study of programminglanguages, since programs can be identified with λ-terms. However, if one goes further anduses βη-conversion to prove equivalence of programs, then a gross simplification is introduced(programs are identified with total functions from values to values), that may jeopardise theapplicability of theoretical results. In this paper we introduce calculi based on a categoricalsemantics for computations, that provide a correct basis for proving equivalence of programs,for a wide range of notions of computation.
IntroductionThis paper is about logics for reasoning about programs, in particular for proving equivalence ofprograms. Following a consolidated tradition in theoretical computer science we identify programswith the closed λ-terms, possibly containing extra constants, corresponding to some features ofthe programming language under consideration. There are three semantic-based approaches toproving equivalence of programs:
• The operational approach starts from an operational semantics, e.g. a partial functionmapping every program (i.e. closed term) to its resulting value (if any), which induces acongruence relation on open terms called operational equivalence (see e.g. [Plo75]). Thenthe problem is to prove that two terms are operationally equivalent.
• The denotational approach gives an interpretation of the (programming) language in amathematical structure, the intended model. Then the problem is to prove that two termsdenote the same object in the intended model.
• The logical approach gives a class of possible models for the (programming) language.Then the problem is to prove that two terms denotes the same object in all possible models.
The operational and denotational approaches give only a theory: the operational equivalence ≈or the set Th of formulas valid in the intended model respectively. On the other hand, the logicalapproach gives a consequence relation ⊢, namely Ax ⊢ A iff the formula A is true in all modelsof the set of formulas Ax, which can deal with different programming languages (e.g. functional,imperative, non-deterministic) in a rather uniform way, by simply changing the set of axiomsAx, and possibly extending the language with new constants. Moreover, the relation ⊢ is oftensemidecidable, so it is possible to give a sound and complete formal system for it, while Th and ≈are semidecidable only in oversimplified cases.
We do not take as a starting point for proving equivalence of programs the theory of βη-conversion, which identifies the denotation of a program (procedure) of type A → B with atotal function from A to B, since this identification wipes out completely behaviours like non-termination, non-determinism or side-effects, that can be exhibited by real programs. Instead, weproceed as follows:
1. We take category theory as a general theory of functions and develop on top a categoricalsemantics of computations based on monads.
∗Research partially supported by EEC Joint Collaboration Contract # ST2J-0374-C(EDB).
1
![Page 11: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/11.jpg)
A Highly Composable Compute Pipeline
34
3 13
646
36
4
MongoDB Aggregation Framework
$sum$match
Notions of computation and monads
Eugenio Moggi∗
Abstract
The λ-calculus is considered an useful mathematical tool in the study of programminglanguages, since programs can be identified with λ-terms. However, if one goes further anduses βη-conversion to prove equivalence of programs, then a gross simplification is introduced(programs are identified with total functions from values to values), that may jeopardise theapplicability of theoretical results. In this paper we introduce calculi based on a categoricalsemantics for computations, that provide a correct basis for proving equivalence of programs,for a wide range of notions of computation.
IntroductionThis paper is about logics for reasoning about programs, in particular for proving equivalence ofprograms. Following a consolidated tradition in theoretical computer science we identify programswith the closed λ-terms, possibly containing extra constants, corresponding to some features ofthe programming language under consideration. There are three semantic-based approaches toproving equivalence of programs:
• The operational approach starts from an operational semantics, e.g. a partial functionmapping every program (i.e. closed term) to its resulting value (if any), which induces acongruence relation on open terms called operational equivalence (see e.g. [Plo75]). Thenthe problem is to prove that two terms are operationally equivalent.
• The denotational approach gives an interpretation of the (programming) language in amathematical structure, the intended model. Then the problem is to prove that two termsdenote the same object in the intended model.
• The logical approach gives a class of possible models for the (programming) language.Then the problem is to prove that two terms denotes the same object in all possible models.
The operational and denotational approaches give only a theory: the operational equivalence ≈or the set Th of formulas valid in the intended model respectively. On the other hand, the logicalapproach gives a consequence relation ⊢, namely Ax ⊢ A iff the formula A is true in all modelsof the set of formulas Ax, which can deal with different programming languages (e.g. functional,imperative, non-deterministic) in a rather uniform way, by simply changing the set of axiomsAx, and possibly extending the language with new constants. Moreover, the relation ⊢ is oftensemidecidable, so it is possible to give a sound and complete formal system for it, while Th and ≈are semidecidable only in oversimplified cases.
We do not take as a starting point for proving equivalence of programs the theory of βη-conversion, which identifies the denotation of a program (procedure) of type A → B with atotal function from A to B, since this identification wipes out completely behaviours like non-termination, non-determinism or side-effects, that can be exhibited by real programs. Instead, weproceed as follows:
1. We take category theory as a general theory of functions and develop on top a categoricalsemantics of computations based on monads.
∗Research partially supported by EEC Joint Collaboration Contract # ST2J-0374-C(EDB).
1
A monad defines what it means to chain operations together.
This allows the programmer to build pipelines that process data in a series of steps […]
Monads allow a programming style where programs are written by putting together highly composable parts […]
![Page 12: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/12.jpg)
A Highly Composable Compute Pipeline
34
3 13
646
36
4
MongoDB Aggregation Framework
$sum$match
Notions of computation and monads
Eugenio Moggi∗
Abstract
The λ-calculus is considered an useful mathematical tool in the study of programminglanguages, since programs can be identified with λ-terms. However, if one goes further anduses βη-conversion to prove equivalence of programs, then a gross simplification is introduced(programs are identified with total functions from values to values), that may jeopardise theapplicability of theoretical results. In this paper we introduce calculi based on a categoricalsemantics for computations, that provide a correct basis for proving equivalence of programs,for a wide range of notions of computation.
IntroductionThis paper is about logics for reasoning about programs, in particular for proving equivalence ofprograms. Following a consolidated tradition in theoretical computer science we identify programswith the closed λ-terms, possibly containing extra constants, corresponding to some features ofthe programming language under consideration. There are three semantic-based approaches toproving equivalence of programs:
• The operational approach starts from an operational semantics, e.g. a partial functionmapping every program (i.e. closed term) to its resulting value (if any), which induces acongruence relation on open terms called operational equivalence (see e.g. [Plo75]). Thenthe problem is to prove that two terms are operationally equivalent.
• The denotational approach gives an interpretation of the (programming) language in amathematical structure, the intended model. Then the problem is to prove that two termsdenote the same object in the intended model.
• The logical approach gives a class of possible models for the (programming) language.Then the problem is to prove that two terms denotes the same object in all possible models.
The operational and denotational approaches give only a theory: the operational equivalence ≈or the set Th of formulas valid in the intended model respectively. On the other hand, the logicalapproach gives a consequence relation ⊢, namely Ax ⊢ A iff the formula A is true in all modelsof the set of formulas Ax, which can deal with different programming languages (e.g. functional,imperative, non-deterministic) in a rather uniform way, by simply changing the set of axiomsAx, and possibly extending the language with new constants. Moreover, the relation ⊢ is oftensemidecidable, so it is possible to give a sound and complete formal system for it, while Th and ≈are semidecidable only in oversimplified cases.
We do not take as a starting point for proving equivalence of programs the theory of βη-conversion, which identifies the denotation of a program (procedure) of type A → B with atotal function from A to B, since this identification wipes out completely behaviours like non-termination, non-determinism or side-effects, that can be exhibited by real programs. Instead, weproceed as follows:
1. We take category theory as a general theory of functions and develop on top a categoricalsemantics of computations based on monads.
∗Research partially supported by EEC Joint Collaboration Contract # ST2J-0374-C(EDB).
1
A monad defines what it means to chain operations together.
This allows the programmer to build pipelines that process data in a series of steps […]
Monads allow a programming style where programs are written by putting together highly composable parts […]
RubyModule Enumerable:collect, select, group_by, …
C# / LINQIEnumerable<T>:Select, Where, GroupBy, …
JavaStream<T>:collect, filter, groupingBy, …
MongoDBAggregation Framework:$project, $match, $group, …
map, filter, group, …
HaskellModule Data.List:
![Page 13: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/13.jpg)
A Highly Composable Compute Pipeline
34
3 13
646
36
4
MongoDB Aggregation Framework
$sum$match
![Page 14: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/14.jpg)
Aggregation Pipelines
(1) are one of the easiest ways to work with your data.
(2) are highly scalable and optimised for performance.
(3) are (much) more capable and powerful than you think!
A Highly Composable Compute Pipeline
34
3 13
646
36
4$sum$match
![Page 15: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/15.jpg)
Aggregation Pipelines
(1) are one of the easiest ways to work with your data.
(2) are highly scalable and optimised for performance.
(3) are (much) more capable and powerful than you think!
![Page 16: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/16.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
The two top paid employees per departement
1 Alex DE 3003 Cora DE 200
DE
US5 Erik US 4006 Fred US 300
NL8 Herb NL 6009 Ivan NL 400
UK
10 Jill UK 500
DB Land
![Page 17: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/17.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
The two top paid employees per departement
1 Alex DE 3003 Cora DE 200
DE
US5 Erik US 4006 Fred US 300
NL8 Herb NL 6009 Ivan NL 400
UK
10 Jill UK 500
DB Land
![Page 18: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/18.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
The two top paid employees per departement
select id, name, dpt, sal from emps where sal =
(select max(sal) from emps as e where e.dpt = emps.dpt)
or sal = (select max(sal) from emps as e where e.dpt = emps.dpt and sal < (select max(sal) from emps as e2 where e2.dpt = emps.dpt)); DB Land
![Page 19: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/19.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
The two top paid employees per departement
select r.id, r.name, r.dpt, r.sal from (select id, name, dept, sal, rank() OVER (PARTITION BY dpt ORDER BY sal DESC) as rank from emps) AS r where r.rank <= 2 order by r.dpt, r.rank
1 Alex DE 3003 Cora DE 2005 Erik US 4006 Fred US 3008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
DB Land
![Page 20: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/20.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
The two top paid employees per departement
1 Alex DE 3003 Cora DE 2005 Erik US 4006 Fred US 3008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL1 Alex DE 3003 Cora DE 200
DE
US5 Erik US 4006 Fred US 300
NL8 Herb NL 6009 Ivan NL 400
UK
10 Jill UK 500
?
DB Land
![Page 21: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/21.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
The two top paid employees per departement
1 Alex DE 3003 Cora DE 2005 Erik US 4006 Fred US 3008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL1 Alex DE 3003 Cora DE 200
DE
US5 Erik US 4006 Fred US 300
NL8 Herb NL 6009 Ivan NL 400
UK
10 Jill UK 500
?
"Data should be easy to work with."
"The data should serve you. You should not serve the data."
"The same (data-) model in your head, in your code, and in your data - NO impedance mismatch."
DB Land
![Page 22: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/22.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
DB LandPL Land
![Page 23: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/23.jpg)
PL Land
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
ID NAME DPT SAL
DB Land
![Page 24: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/24.jpg)
PL Land
{"id"=>10, "name"=>"Jill", "dpt"=>"UK", "sal"=>500}]] {"id"=> 9, "name"=>"Ivan", "dpt"=>"NL", "sal"=>400},, {"id"=> 8, "name"=>"Herb", "dpt"=>"NL", "sal"=>600},, {"id"=> 7, "name"=>"Gina", "dpt"=>"US", "sal"=>200},, {"id"=> 6, "name"=>"Fred", "dpt"=>"US", "sal"=>300},, {"id"=> 5, "name"=>"Erik", "dpt"=>"US", "sal"=>400},, {"id"=> 4, "name"=>"Drew", "dpt"=>"US", "sal"=>200},, {"id"=> 3, "name"=>"Cora", "dpt"=>"DE", "sal"=>200},, {"id"=> 2, "name"=>"Bert", "dpt"=>"DE", "sal"=>100},,[{"id"=> 1, "name"=>"Alex", "dpt"=>"DE", "sal"=>300},,[
DB Land
![Page 25: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/25.jpg)
PL Land
{"id"=>10, "name"=>"Jill", "dpt"=>"UK", "sal"=>500}]] {"id"=> 9, "name"=>"Ivan", "dpt"=>"NL", "sal"=>400},, {"id"=> 8, "name"=>"Herb", "dpt"=>"NL", "sal"=>600},, {"id"=> 7, "name"=>"Gina", "dpt"=>"US", "sal"=>200},, {"id"=> 6, "name"=>"Fred", "dpt"=>"US", "sal"=>300},, {"id"=> 5, "name"=>"Erik", "dpt"=>"US", "sal"=>400},, {"id"=> 4, "name"=>"Drew", "dpt"=>"US", "sal"=>200},, {"id"=> 3, "name"=>"Cora", "dpt"=>"DE", "sal"=>200},, {"id"=> 2, "name"=>"Bert", "dpt"=>"DE", "sal"=>100},,[{"id"=> 1, "name"=>"Alex", "dpt"=>"DE", "sal"=>300},,[
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}collect
group_bysort_by
take
Ruby
DB Land
![Page 26: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/26.jpg)
PL Land
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}collect
group_bysort_by
take
{"id"=>10, "name"=>"Jill", "dpt"=>"UK", "sal"=>500}]] {"id"=> 9, "name"=>"Ivan", "dpt"=>"NL", "sal"=>400},, {"id"=> 8, "name"=>"Herb", "dpt"=>"NL", "sal"=>600},, {"id"=> 7, "name"=>"Gina", "dpt"=>"US", "sal"=>200},,
{"id"=> 6, "name"=>"Fred", "dpt"=>"US", "sal"=>300},, {"id"=> 5, "name"=>"Erik", "dpt"=>"US", "sal"=>400},,
{"id"=> 4, "name"=>"Drew", "dpt"=>"US", "sal"=>200},,
{"id"=> 3, "name"=>"Cora", "dpt"=>"DE", "sal"=>200},, {"id"=> 2, "name"=>"Bert", "dpt"=>"DE", "sal"=>100},,
[{"id"=> 1, "name"=>"Alex", "dpt"=>"DE", "sal"=>300},,[
Ruby
DB Land
![Page 27: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/27.jpg)
PL Land
},
[{"DE"=>
]
]
]
[
[
[
[{"US"=>
[{"NL"=>
[{"UK"=>
},
},
]
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}collect
group_bysort_by
take
{"id"=>10, "name"=>"Jill", "dpt"=>"UK", "sal"=>500}]]
{"id"=> 9, "name"=>"Ivan", "dpt"=>"NL", "sal"=>400},, {"id"=> 8, "name"=>"Herb", "dpt"=>"NL", "sal"=>600},,
{"id"=> 7, "name"=>"Gina", "dpt"=>"US", "sal"=>200},,
{"id"=> 6, "name"=>"Fred", "dpt"=>"US", "sal"=>300},, {"id"=> 5, "name"=>"Erik", "dpt"=>"US", "sal"=>400},,
{"id"=> 4, "name"=>"Drew", "dpt"=>"US", "sal"=>200},,
{"id"=> 3, "name"=>"Cora", "dpt"=>"DE", "sal"=>200},, {"id"=> 2, "name"=>"Bert", "dpt"=>"DE", "sal"=>100},,
[{"id"=> 1, "name"=>"Alex", "dpt"=>"DE", "sal"=>300},,[
Ruby
DB Land
![Page 28: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/28.jpg)
PL Land
}, {"id"=> 9, "name"=>"Ivan", "dpt"=>"NL", "sal"=>400},, {"id"=> 8, "name"=>"Herb", "dpt"=>"NL", "sal"=>600},,[[{"NL"=>
{"id"=> 6, "name"=>"Fred", "dpt"=>"US", "sal"=>300},, {"id"=> 5, "name"=>"Erik", "dpt"=>"US", "sal"=>400},,[[{"US"=>
},
[{"DE"=> {"id"=> 3, "name"=>"Cora", "dpt"=>"DE", "sal"=>200},,[{"id"=> 1, "name"=>"Alex", "dpt"=>"DE", "sal"=>300},,[
},
{"id"=>10, "name"=>"Jill", "dpt"=>"UK", "sal"=>500}]][[{"UK"=> ]
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}collect
group_bysort_by
take
]
]]
]]
Ruby
DB Land
![Page 29: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/29.jpg)
PL Land
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}takecollect
group_bysort_by
}, {"id"=> 9, "name"=>"Ivan", "dpt"=>"NL", "sal"=>400},, {"id"=> 8, "name"=>"Herb", "dpt"=>"NL", "sal"=>600},,
][[{"NL"=>
{"id"=> 6, "name"=>"Fred", "dpt"=>"US", "sal"=>300},, {"id"=> 5, "name"=>"Erik", "dpt"=>"US", "sal"=>400},,
][[{"US"=>
},
[{"DE"=> {"id"=> 3, "name"=>"Cora", "dpt"=>"DE", "sal"=>200},,[{"id"=> 1, "name"=>"Alex", "dpt"=>"DE", "sal"=>300},,[
] },
{"id"=>10, "name"=>"Jill", "dpt"=>"UK", "sal"=>500}]][[{"UK"=> ]
1 Alex DE 3003 Cora DE 200
DE
US5 Erik US 4006 Fred US 300
NL8 Herb NL 6009 Ivan NL 400
UK
10 Jill UK 500
Ruby
DB Land
![Page 30: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/30.jpg)
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}collect
group_bysort_by
Ruby
PL Land
emps.sorted(Comparator.comparing(Employee::getDpt) .thenComparing(Employee::getSal) .reversed()).collect(Collectors .groupingBy(Employee::getDpt, Collectors .collectingAndThen(Collectors.toList(),l->l.stream() .limit(2).collect( Collectors.toList()))));
emps.OrderBy(e => e.Dpt).ThenByDescending(e => e.Sal) .GroupBy(e => e.Dpt) .Select(g => new {Key = g.Key, Emps = g.Take(2)})
C# / LINQ
Java Streams
take
DB Land
![Page 31: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/31.jpg)
PL Land
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}takecollect
group_by$sort
Ruby
DB Land
![Page 32: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/32.jpg)
PL Land
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}takecollect
$sort$group
Ruby
DB Land
![Page 33: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/33.jpg)
PL Land
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}take
$sort$group$project
Ruby
DB Land
![Page 34: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/34.jpg)
PL Land
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}
$sort$group$project $slice
Ruby
DB Land
![Page 35: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/35.jpg)
PL Land
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
Aggregation Pipeline
DB Land
![Page 36: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/36.jpg)
PL Land
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
Aggregation Pipeline
DB Land
$project
$slice$group$sort
![Page 37: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/37.jpg)
MongoDB
PL Land
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
Aggregation Pipeline
PL Land DB Land
$project
$slice$group$sort
![Page 38: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/38.jpg)
MongoDB
PL Land
{"_id":10, "name":"Jill", "dpt":"UK", "sal":500}] {"_id": 9, "name":"Ivan", "dpt":"NL", "sal":400}, {"_id": 8, "name":"Herb", "dpt":"NL", "sal":600}, {"_id": 7, "name":"Gina", "dpt":"US", "sal":200}, {"_id": 6, "name":"Fred", "dpt":"US", "sal":300}, {"_id": 5, "name":"Erik", "dpt":"US", "sal":400}, {"_id": 4, "name":"Drew", "dpt":"US", "sal":200}, {"_id": 3, "name":"Cora", "dpt":"DE", "sal":200}, {"_id": 2, "name":"Bert", "dpt":"DE", "sal":100},[{"_id": 1, "name":"Alex", "dpt":"DE", "sal":300},
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
Aggregation Pipeline
DB Land
![Page 39: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/39.jpg)
MongoDB
PL Land
{"_id":10, "name":"Jill", "dpt":"UK", "sal":500}] {"_id": 9, "name":"Ivan", "dpt":"NL", "sal":400}, {"_id": 8, "name":"Herb", "dpt":"NL", "sal":600}, {"_id": 7, "name":"Gina", "dpt":"US", "sal":200},
{"_id": 6, "name":"Fred", "dpt":"US", "sal":300}, {"_id": 5, "name":"Erik", "dpt":"US", "sal":400},
{"_id": 4, "name":"Drew", "dpt":"US", "sal":200},
{"_id": 3, "name":"Cora", "dpt":"DE", "sal":200}, {"_id": 2, "name":"Bert", "dpt":"DE", "sal":100},
[{"_id": 1, "name":"Alex", "dpt":"DE", "sal":300},
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
Aggregation Pipeline
DB Land
![Page 40: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/40.jpg)
MongoDB
PL Land
{"_id":10, "name":"Jill", "dpt":"UK", "sal":500}]
{"_id": 9, "name":"Ivan", "dpt":"NL", "sal":400}, {"_id": 8, "name":"Herb", "dpt":"NL", "sal":600},
{"_id": 7, "name":"Gina", "dpt":"US", "sal":200},
{"_id": 6, "name":"Fred", "dpt":"US", "sal":300}, {"_id": 5, "name":"Erik", "dpt":"US", "sal":400},
{"_id": 4, "name":"Drew", "dpt":"US", "sal":200},
{"_id": 3, "name":"Cora", "dpt":"DE", "sal":200}, {"_id": 2, "name":"Bert", "dpt":"DE", "sal":100},
[{"_id": 1, "name":"Alex", "dpt":"DE", "sal":300},[{"_id" :"DE", "emps":
}, {"_id" :"US", "emps":
}, {"_id" :"NL", "emps":
}, {"_id" :"UK", "emps":
]
]
]
}]
[
[
[
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
Aggregation Pipeline
DB Land
![Page 41: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/41.jpg)
MongoDB
PL Land
{"_id": 3, "name":"Cora", "dpt":"DE", "sal":200},[{"_id": 1, "name":"Alex", "dpt":"DE", "sal":300},
[{"dpt" :"DE", "emps":
},]
{"_id": 6, "name":"Fred", "dpt":"US", "sal":300}, {"_id": 5, "name":"Erik", "dpt":"US", "sal":400},
{"dpt" :"US", "emps":
},]
{"_id": 9, "name":"Ivan", "dpt":"NL", "sal":400}, {"_id": 8, "name":"Herb", "dpt":"NL", "sal":600},
{"dpt" :"NL", "emps":
},]
{"_id":10, "name":"Jill", "dpt":"UK", "sal":500}] {"dpt" :"UK", "emps": }]
[
[
[
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
Aggregation Pipeline
DB Land
![Page 42: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/42.jpg)
MongoDB
PL Land
{"_id": 3, "name":"Cora", "dpt":"DE", "sal":200},[{"_id": 1, "name":"Alex", "dpt":"DE", "sal":300},
[{"dpt" :"DE", "emps":
},]
{"_id": 6, "name":"Fred", "dpt":"US", "sal":300}, {"_id": 5, "name":"Erik", "dpt":"US", "sal":400},
{"dpt" :"US", "emps":
},]
{"_id": 9, "name":"Ivan", "dpt":"NL", "sal":400}, {"_id": 8, "name":"Herb", "dpt":"NL", "sal":600},
{"dpt" :"NL", "emps":
},]
{"_id":10, "name":"Jill", "dpt":"UK", "sal":500}] {"dpt" :"UK", "emps": }]
[
[
[
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
1 Alex DE 3003 Cora DE 200
DE
US5 Erik US 4006 Fred US 300
NL8 Herb NL 6009 Ivan NL 400
UK
10 Jill UK 500
Aggregation Pipeline
DB Land
![Page 43: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/43.jpg)
MongoDB
PL Land
{"_id": 3, "name":"Cora", "dpt":"DE", "sal":200},[{"_id": 1, "name":"Alex", "dpt":"DE", "sal":300},
[{"dpt" :"DE", "emps":
},]
{"_id": 6, "name":"Fred", "dpt":"US", "sal":300}, {"_id": 5, "name":"Erik", "dpt":"US", "sal":400},
{"dpt" :"US", "emps":
},]
{"_id": 9, "name":"Ivan", "dpt":"NL", "sal":400}, {"_id": 8, "name":"Herb", "dpt":"NL", "sal":600},
{"dpt" :"NL", "emps":
},]
{"_id":10, "name":"Jill", "dpt":"UK", "sal":500}] {"dpt" :"UK", "emps": }]
[
[
[
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
1 Alex DE 3003 Cora DE 200
DE
US5 Erik US 4006 Fred US 300
NL8 Herb NL 6009 Ivan NL 400
UK
10 Jill UK 500
"Data should be easy to work with."
"The data should serve you. You should not serve the data."
"The same (data-) model in your head, in your code, and in your data - NO impedance mismatch."
Aggregation Pipeline
DB Land
![Page 44: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/44.jpg)
MongoDB
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ])
$sort$group$project $slice
Aggregation Pipeline
emps.OrderBy(e => e.Dpt).ThenByDescending(e => e.Sal) .GroupBy(e => e.Dpt) .Select(g => new {Key = g.Key, Emps = g.Take(2)})
C#
emps.sorted(Comparator.comparing(Employee::getDpt) .thenComparing(Employee::getSal) .reversed()).collect(Collectors .groupingBy(Employee::getDpt, Collectors .collectingAndThen(Collectors.toList(),l->l.stream() .limit(2).collect( Collectors.toList()))));
Java
emps. {|e| [e["dpt"], -e["sal"]]} . {|e| e["dpt"]} . {|k,v| [k => v. (2)]}collect
group_bysort_by Ruby
take
DB LandPL Land
![Page 45: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/45.jpg)
Aggregation Pipelines
(1) are one of the easiest ways to work with your data.
(2) are highly scalable and optimised for performance.
(3) are (much) more capable and powerful than you think!
![Page 46: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/46.jpg)
Benchmarking PL Land vs DB Land
2,2 GHz Intel Core i7 CPU 16 GB 1600 MHz DDR3 RAM SSD-Storage
MongoDB Enterprise 3.4.0-rc0 WiredTiger (12 GB Cache Size,
snappy compressor)
!
![Page 47: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/47.jpg)
λ
PL Land DB Land
![Page 48: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/48.jpg)
λ
PL Land DB Land
# Employees Data Size Index Size Runtime
1M 50 MB 20 MB 6 s + 7 s
10M 500 MB 200 MB 133 s + 99 s
100M 5 GB 2 GB 2 h - OOM
![Page 49: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/49.jpg)
PL Land DB Land
Q
![Page 50: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/50.jpg)
PL Land DB Land
Q
# Employees Data Size Index Size Runtime Runtime
1M 50 MB 20 MB 6 s + 7 s 100 ms
10M 500 MB 200 MB 133 s + 99 s 1 s
100M 5 GB 2 GB 2 h - OOM 5 m
![Page 51: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/51.jpg)
PL Land DB Land
Q
100M 5 GB 2 GB 2 h - OOM 5 m
![Page 52: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/52.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ]
$slice)
$project$group$sort
$project$group$sort
5 GB
![Page 53: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/53.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ]
$slice)
$project$group$sort
$project$group$sort
5 GB"errmsg":"Exceededmemorylimitfor$group,butdidn'tallowexternalsort.PassallowDiskUse:truetooptin."
![Page 54: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/54.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ]
$slice)
$project$group$sort
$project$group$sort
5 GB,{allowDiskUse:true}
![Page 55: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/55.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
db.emps.aggregate([ { : {dpt:1, sal:-1}}, { : {_id:"$dpt", emps:{$push: "$$CURRENT"}}}, { : {dpt:"$_id", emps:{ : ["$emps",2]}}} ]
$slice)
$project$group$sort
$project$group$sort
5 GB,{allowDiskUse:true}
!CPU Load
![Page 56: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/56.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
$project$group$sort
5 GB
mongod --dbpath ~/data/shard1 --port 40001 mongod --dbpath ~/data/shard2 --port 40002 … mongod --dbpath ~/data/shard10 --port 40010
!
![Page 57: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/57.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
$project$group$sort
5 GB
mongod --dbpath ~/data/shard1 --port 40001 mongod --dbpath ~/data/shard2 --port 40002 … mongod --dbpath ~/data/shard10 --port 40010
!
![Page 58: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/58.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
$project$group$sort
5 GB
mongod --dbpath ~/data/shard1 --port 40001 mongod --dbpath ~/data/shard2 --port 40002 … mongod --dbpath ~/data/shard10 --port 40010
!sh.addShard("Toms-MacBook-Pro.local:40001") sh.addShard("Toms-MacBook-Pro.local:40002") … sh.addShard("Toms-MacBook-Pro.local:40010")
![Page 59: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/59.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
$project$group$sort
5 GB
mongod --dbpath ~/data/shard1 --port 40001 mongod --dbpath ~/data/shard2 --port 40002 … mongod --dbpath ~/data/shard10 --port 40010
!sh.addShard("Toms-MacBook-Pro.local:40001") sh.addShard("Toms-MacBook-Pro.local:40002") … sh.addShard("Toms-MacBook-Pro.local:40010")
sh.shardCollection("DB.emps", {dpt:1})
![Page 60: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/60.jpg)
!DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
$project$group$sort
5 GB
mongod --dbpath ~/data/shard1 --port 40001 mongod --dbpath ~/data/shard2 --port 40002 … mongod --dbpath ~/data/shard10 --port 40010
!sh.addShard("Toms-MacBook-Pro.local:40001") sh.addShard("Toms-MacBook-Pro.local:40002") … sh.addShard("Toms-MacBook-Pro.local:40010")
sh.shardCollection("DB.emps", {dpt:1})
"Micro-Sharding"0.5 GB0.5 GB0.5 GB0.5 GB0.5 GB
0.5 GB 0.5 GB 0.5 GB 0.5 GB 0.5 GB
![Page 61: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/61.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
$project$group$sort
5 GB
!
![Page 62: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/62.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
$project$group$sort
0.5 GB0.5 GB 0.5 GB0.5 GB
0.5 GB0.5 GB0.5 GB0.5 GB0.5 GB
0.5 GB$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
!
![Page 63: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/63.jpg)
DB Land
100M 5 GB 2 GB 2 h - OOM 5 m
$project$group$sort
0.5 GB0.5 GB 0.5 GB0.5 GB
0.5 GB0.5 GB0.5 GB0.5 GB0.5 GB
0.5 GB$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
$project$group$sort
6 s
!CPU Load
!
![Page 64: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/64.jpg)
DB Land
{"name":"Cora", "dpt":"DE", "sal":200},{"name":"Alex", "dpt":"DE", "sal":300},
[{"dpt" :"DE", "emps":
},]
{"name":"Fred", "dpt":"US", "sal":300},{"name":"Erik", "dpt":"US", "sal":400},
{"dpt" :"US", "emps":
},]
{"name":"Ivan", "dpt":"NL", "sal":400},{"name":"Herb", "dpt":"NL", "sal":600},
{"dpt" :"NL", "emps":
},]
{"name":"Jill", "dpt":"UK", "sal":500}] {"dpt" :"UK", "emps" }]
[
[
[
db.emps.aggregate([
[
{$sort: {dpt:1,sal:-1}}, {$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}])
![Page 65: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/65.jpg)
DB Land
{"name":"Cora", "dpt":"DE", "sal":200},{"name":"Alex", "dpt":"DE", "sal":300},
[{"dpt" :"DE", "emps":
},]
{"name":"Fred", "dpt":"US", "sal":300},{"name":"Erik", "dpt":"US", "sal":400},
{"dpt" :"US", "emps":
},]
{"name":"Ivan", "dpt":"NL", "sal":400},{"name":"Herb", "dpt":"NL", "sal":600},
{"dpt" :"NL", "emps":
},]
{"name":"Jill", "dpt":"UK", "sal":500}] {"dpt" :"UK", "emps" }]
[
[
[
db.emps.aggregate([
[
{$sort: {dpt:1,sal:-1}}, {$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}} {$match: {dpt:"DE"}},])
![Page 66: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/66.jpg)
DB Land
{"name":"Cora", "dpt":"DE", "sal":200},{"name":"Alex", "dpt":"DE", "sal":300},
[{"dpt" :"DE", "emps":
},]
{"name":"Fred", "dpt":"US", "sal":300},{"name":"Erik", "dpt":"US", "sal":400},
{"dpt" :"US", "emps":
},]
{"name":"Ivan", "dpt":"NL", "sal":400},{"name":"Herb", "dpt":"NL", "sal":600},
{"dpt" :"NL", "emps":
},]
{"name":"Jill", "dpt":"UK", "sal":500}] {"dpt" :"UK", "emps" }]
[
[
[
db.emps.aggregate([
[
{$sort: {dpt:1,sal:-1}}, {$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}},
{$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}} {$match: {dpt:"DE"}},
])
![Page 67: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/67.jpg)
DB Land
{"name":"Cora", "dpt":"DE", "sal":200},{"name":"Alex", "dpt":"DE", "sal":300},
[{"dpt" :"DE", "emps":
},]
{"name":"Fred", "dpt":"US", "sal":300},{"name":"Erik", "dpt":"US", "sal":400},
{"dpt" :"US", "emps":
},]
{"name":"Ivan", "dpt":"NL", "sal":400},{"name":"Herb", "dpt":"NL", "sal":600},
{"dpt" :"NL", "emps":
},]
{"name":"Jill", "dpt":"UK", "sal":500}] {"dpt" :"UK", "emps" }]
[
[
[
db.emps.aggregate([
[
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
])
![Page 68: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/68.jpg)
DB Land
{"name":"Cora", "dpt":"DE", "sal":200},{"name":"Alex", "dpt":"DE", "sal":300},
[{"dpt" :"DE", "emps":
},]
{"name":"Fred", "dpt":"US", "sal":300},{"name":"Erik", "dpt":"US", "sal":400},
{"dpt" :"US", "emps":
},]
{"name":"Ivan", "dpt":"NL", "sal":400},{"name":"Herb", "dpt":"NL", "sal":600},
{"dpt" :"NL", "emps":
},]
{"name":"Jill", "dpt":"UK", "sal":500}] {"dpt" :"UK", "emps" }]
[
[
[
db.emps.aggregate([
]
[
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
![Page 69: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/69.jpg)
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
![Page 70: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/70.jpg)
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
![Page 71: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/71.jpg)
{ "stages": [ { "$cursor": { "query": { "dpt": "DE" }, "sort": { "dpt": 1, "sal": -1 }, "fields": { "dpt": 1, "name": 1, "sal": 1,
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
![Page 72: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/72.jpg)
{ "stages": [ { "$cursor": { "query": { "dpt": "DE" }, "sort": { "dpt": 1, "sal": -1 }, "fields": { "dpt": 1, "name": 1, "sal": 1, "_id": 0 }, "queryPlanner": {
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
![Page 73: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/73.jpg)
{ "stages": [ { "$cursor": { "query": { "dpt": "DE" }, "sort": { "dpt": 1, "sal": -1 }, "fields": { "dpt": 1, "name": 1, "sal": 1, "_id": 0 }, "queryPlanner": {
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$cursor
![Page 74: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/74.jpg)
], "name": [ "[MinKey, MaxKey]" ] } } }, "rejectedPlans": [] } } }, { "$group": { "_id": "$dpt", "emps": { "$push": { "name": "$name", "sal": "$sal" } } } }, { "$project": {
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$cursor
![Page 75: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/75.jpg)
], "name": [ "[MinKey, MaxKey]" ] } } }, "rejectedPlans": [] } } }, { "$group": { "_id": "$dpt", "emps": { "$push": { "name": "$name", "sal": "$sal" } } } }, { "$project": {
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$cursor
![Page 76: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/76.jpg)
"_id": "$dpt", "emps": { "$push": { "name": "$name", "sal": "$sal" } } } }, { "$project": { "_id": false, "dpt": "$_id", "emps": { "$slice": [ "$emps", { "$const": 2 } ] } } } ],
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$cursor
![Page 77: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/77.jpg)
"_id": "$dpt", "emps": { "$push": { "name": "$name", "sal": "$sal" } } } }, { "$project": { "_id": false, "dpt": "$_id", "emps": { "$slice": [ "$emps", { "$const": 2 } ] } } } ],
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
![Page 78: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/78.jpg)
"queryPlanner": { "plannerVersion": 1, "namespace": "DB.emps", "indexFilterSet": false, "parsedQuery": { "dpt": { "$eq": "DE" } }, "winningPlan": { "stage": "PROJECTION", "transformBy": { "dpt": 1, "name": 1, "sal": 1, "_id": 0 }, "inputStage": { "stage": "IXSCAN", "keyPattern": { "dpt": 1, "sal": -1, "name": 1 },
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
![Page 79: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/79.jpg)
"queryPlanner": { "plannerVersion": 1, "namespace": "DB.emps", "indexFilterSet": false, "parsedQuery": { "dpt": { "$eq": "DE" } }, "winningPlan": { "stage": "PROJECTION", "transformBy": { "dpt": 1, "name": 1, "sal": 1, "_id": 0 }, "inputStage": { "stage": "IXSCAN", "keyPattern": { "dpt": 1, "sal": -1, "name": 1 },
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
IXSCAN
Index on "dpt":1, "sal":-1, "name":1
PROJECTION
![Page 80: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/80.jpg)
"multiKeyPaths": { "dpt": [], "sal": [], "name": [] }, "isUnique": false, "isSparse": false, "isPartial": false, "indexVersion": 2, "direction": "forward", "indexBounds": { "dpt": [ "[\"DE\", \"DE\"]" ], "sal": [ "[MaxKey, MinKey]" ], "name": [ "[MinKey, MaxKey]" ] } } }, "rejectedPlans": []
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
IXSCAN
Index on "dpt":1, "sal":-1, "name":1
PROJECTION
![Page 81: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/81.jpg)
"multiKeyPaths": { "dpt": [], "sal": [], "name": [] }, "isUnique": false, "isSparse": false, "isPartial": false, "indexVersion": 2, "direction": "forward", "indexBounds": { "dpt": [ "[\"DE\", \"DE\"]" ], "sal": [ "[MaxKey, MinKey]" ], "name": [ "[MinKey, MaxKey]" ] } } }, "rejectedPlans": []
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
IXSCAN
Index on "dpt":1, "sal":-1, "name":1
PROJECTION
Index used for sorting
![Page 82: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/82.jpg)
"multiKeyPaths": { "dpt": [], "sal": [], "name": [] }, "isUnique": false, "isSparse": false, "isPartial": false, "indexVersion": 2, "direction": "forward", "indexBounds": { "dpt": [ "[\"DE\", \"DE\"]" ], "sal": [ "[MaxKey, MinKey]" ], "name": [ "[MinKey, MaxKey]" ] } } }, "rejectedPlans": []
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
IXSCAN
Index on "dpt":1, "sal":-1, "name":1
PROJECTION
Index used for sorting
Pipeline Reordering:
Index used for matching
![Page 83: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/83.jpg)
"queryPlanner": { "plannerVersion": 1, "namespace": "DB.emps", "indexFilterSet": false, "parsedQuery": { "dpt": { "$eq": "DE" } }, "winningPlan": { "stage": "PROJECTION", "transformBy": { "dpt": 1, "name": 1, "sal": 1, "_id": 0 }, "inputStage": { "stage": "IXSCAN", "keyPattern": { "dpt": 1, "sal": -1, "name": 1 },
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
IXSCAN
Index on "dpt":1, "sal":-1, "name":1
PROJECTION
Index used for sorting
Pipeline Reordering:
Index used for matching
![Page 84: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/84.jpg)
"queryPlanner": { "plannerVersion": 1, "namespace": "DB.emps", "indexFilterSet": false, "parsedQuery": { "dpt": { "$eq": "DE" } }, "winningPlan": { "stage": "PROJECTION", "transformBy": { "dpt": 1, "name": 1, "sal": 1, "_id": 0 }, "inputStage": { "stage": "IXSCAN", "keyPattern": { "dpt": 1, "sal": -1, "name": 1 },
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
IXSCAN
Index on "dpt":1, "sal":-1, "name":1
PROJECTION
Index used for sorting
Pipeline Reordering:
Index used for matching
Projection Optimization:
Query turned to Covered Query
![Page 85: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/85.jpg)
"queryPlanner": { "plannerVersion": 1, "namespace": "DB.emps", "indexFilterSet": false, "parsedQuery": { "dpt": { "$eq": "DE" } }, "winningPlan": { "stage": "PROJECTION", "transformBy": { "dpt": 1, "name": 1, "sal": 1, "_id": 0 }, "inputStage": { "stage": "IXSCAN", "keyPattern": { "dpt": 1, "sal": -1, "name": 1 },
db.emps.aggregate([
]
{$sort: {dpt:1,sal:-1}},
{$group: {_id:"$dpt",emps:{$push:{name:"$name",sal:"$sal"}}}}, {$project: {dpt:"$_id",emps:{$slice :["$emps",2]}}}
{$match: {dpt:"DE"}},
,{explain:true})
DB Land
$group
$project
$cursor
IXSCAN
Index on "dpt":1, "sal":-1, "name":1
PROJECTION
Index used for sorting
Pipeline Reordering:
Index used for matching
Projection Optimization:
Query turned to Covered Query
![Page 86: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/86.jpg)
Pipeline Reordering:
Index used
Projection Optimization:
Query turned to
DB Land
Aggregation Pipelines
(1) are one of the easiest ways to work with your data.
(2) are highly scalable and optimised for performance.
(3) are (much) more capable and powerful than you think!
![Page 87: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/87.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
DB Land
_id name dpt sal
![Page 88: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/88.jpg)
1 Alex DE 3002 Bert DE 1003 Cora DE 2004 Drew US 2005 Erik US 4006 Fred US 3007 Gina US 2008 Herb NL 6009 Ivan NL 40010 Jill UK 500
DB Land
_id name dpt sal103161055108
bid
![Page 89: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/89.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
![Page 90: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/90.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
$match $graphLookup
![Page 91: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/91.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 92: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/92.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{ }2 Bert DE 100 3_id name dpt sal bid
"bosses":[, ]
3 Cora DE 200 1_id name dpt sal bid
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 93: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/93.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{ }2 Bert DE 100 3_id name dpt sal bid
"bosses":[, ]
3 Cora DE 200 1_id name dpt sal bid
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 94: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/94.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{ }2 Bert DE 100 3_id name dpt sal bid
"bosses":[, ]
3 Cora DE 200 1_id name dpt sal bid
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 95: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/95.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{ }2 Bert DE 100 3_id name dpt sal bid
"bosses":[, ]
3 Cora DE 200 1_id name dpt sal bid
"
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 96: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/96.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{ }2 Bert DE 100 3_id name dpt sal bid
"bosses":[, ]3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 97: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/97.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{ }2 Bert DE 100 3_id name dpt sal bid
"bosses":[, ]3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
1 Alex DE 300 10_id name dpt sal bid
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 98: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/98.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{ }2 Bert DE 100 3_id name dpt sal bid
"bosses":[, ]3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
1 Alex DE 300 10_id name dpt sal bid
"
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 99: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/99.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{
}
2 Bert DE 100 3_id name dpt sal bid
"bosses":[,
]
3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
1 Alex DE 300 10_id name dpt sal bid
"lvl":1,{ }
,
db.emps.aggregate([
])
Bert’s bosses
{$match: {name:"Bert"}}, {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 100: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/100.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{
}
2 Bert DE 100 3_id name dpt sal bid
"bosses":[,
]
3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
1 Alex DE 300 10_id name dpt sal bid
"lvl":1,{ }
,
10 Jill UK 500_id name dpt sal bid
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 101: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/101.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{
}
2 Bert DE 100 3_id name dpt sal bid
"bosses":[,
]
3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
1 Alex DE 300 10_id name dpt sal bid
"lvl":1,{ }
,
10 Jill UK 500_id name dpt sal bid
"
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 102: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/102.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{
}
2 Bert DE 100 3_id name dpt sal bid
"bosses":[,
]
3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
1 Alex DE 300 10_id name dpt sal bid
"lvl":1,{ }
,
10 Jill UK 500_id name dpt sal bid
"lvl":2,{ }
,
{$match: {name:"Bert"}},db.emps.aggregate([
])
Bert’s bosses
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 103: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/103.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
Bert’s bosses
{
}
2 Bert DE 100 3_id name dpt sal bid
"bosses":[,
]
3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
1 Alex DE 300 10_id name dpt sal bid
"lvl":1,{ }
,
10 Jill UK 500_id name dpt sal bid
"lvl":2,{ }
,
{$match: {name:"Bert"}},db.emps.aggregate([
])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 104: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/104.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
Bert’s bosses
{
}
2 Bert DE 100 3_id name dpt sal bid
"bosses":[,
]
3 Cora DE 200 1_id name dpt sal bid
"lvl":0,{ }
1 Alex DE 300 10_id name dpt sal bid
"lvl":1,{ }
,
10 Jill UK 500_id name dpt sal bid
"lvl":2,{ }
,
{$match: {name:"Bert"}},db.emps.aggregate([
])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 105: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/105.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
Bert’s bosses
{$match: {name:"Bert"}},db.emps.aggregate([
])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
![Page 106: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/106.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
The two employees bosseswith most
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
])
db.emps.aggregate([
, {$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
$limit$graphLookup $sort
![Page 107: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/107.jpg)
8 Herb NL 600 10
10 Jill UK 500
5 Erik US 400 105 Erik US 400 10
10 Jill UK 500
6 Fred US 300 5
1 Alex DE 300 10
3 Cora DE 200 1
10 Jill UK 50010 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[]}]
]},]},]},]},]},]},]},]},]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 108: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/108.jpg)
8 Herb NL 600 10
10 Jill UK 500
5 Erik US 400 105 Erik US 400 10
10 Jill UK 500
6 Fred US 300 5
1 Alex DE 300 10
3 Cora DE 200 1
10 Jill UK 50010 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[]}]
]},]},]},]},]},]},]},]},]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 109: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/109.jpg)
8 Herb NL 600 10
10 Jill UK 500
5 Erik US 400 105 Erik US 400 10
10 Jill UK 500
6 Fred US 300 5
1 Alex DE 300 10
3 Cora DE 200 1
10 Jill UK 50010 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[{ ,"bosses":[]}]
]},]},]},]},]},]},]},]},]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 110: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/110.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}[{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}
10 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[]}]
3 Cora DE 200 11 Alex DE 300 106 Fred US 300 510 Jill UK 5005 Erik US 400 105 Erik US 400 1010 Jill UK 5008 Herb NL 600 10
]},]},]},]},]},]},]},]},]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 111: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/111.jpg)
10 Jill UK 50010 Jill UK 50010 Jill UK 500
5 Erik US 400 10
10 Jill UK 500
1 Alex DE 300 10
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}[{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}
10 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[{ ,"lvl":0}{ ,"bosses":[]}]
3 Cora DE 200 11 Alex DE 300 106 Fred US 300 510 Jill UK 5005 Erik US 400 105 Erik US 400 1010 Jill UK 5008 Herb NL 600 10
]},]},]},
]},]},
]},]},
]},
]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 112: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/112.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}[{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
10 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
{ ,"bosses":[{ ,"lvl":0}{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
{ ,"bosses":[]}]
3 Cora DE 200 1 1 Alex DE 300 101 Alex DE 300 10 10 Jill UK 5006 Fred US 300 5 5 Erik US 400 1010 Jill UK 5005 Erik US 400 10 10 Jill UK 5005 Erik US 400 10 10 Jill UK 50010 Jill UK 5008 Herb NL 600 10 10 Jill UK 500
]},
]},
]},]},
]},]},
]},]},
]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 113: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/113.jpg)
10 Jill UK 50010 Jill UK 50010 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}[{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
10 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
{ ,"bosses":[{ ,"lvl":0}{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
{ ,"bosses":[]}]
3 Cora DE 200 1 1 Alex DE 300 101 Alex DE 300 10 10 Jill UK 5006 Fred US 300 5 5 Erik US 400 1010 Jill UK 5005 Erik US 400 10 10 Jill UK 5005 Erik US 400 10 10 Jill UK 50010 Jill UK 5008 Herb NL 600 10 10 Jill UK 500
]},
]},
]},]},
]},]},
]},]},
]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 114: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/114.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[{ ,"lvl":0} }] ,[{ ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,
10 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
{ ,"bosses":[{ ,"lvl":0}{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
{ ,"bosses":[]}]
3 Cora DE 200 1 1 Alex DE 300 10 10 Jill UK 5001 Alex DE 300 10 10 Jill UK 5006 Fred US 300 5 5 Erik US 400 10 10 Jill UK 50010 Jill UK 5005 Erik US 400 10 10 Jill UK 5005 Erik US 400 10 10 Jill UK 50010 Jill UK 5008 Herb NL 600 10 10 Jill UK 500
}] ,
}] ,}] ,
]},
]},]},
]},]},
]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 115: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/115.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
{ ,"bosses":[{ ,"lvl":0} }] ,[{ ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,
10 Jill UK 500
{ ,"bosses":[{ ,"lvl":0}{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
{ ,"bosses":[{ ,"lvl":0}{ ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},
{ ,"bosses":[]}]
3 Cora DE 200 1 1 Alex DE 300 10 10 Jill UK 5001 Alex DE 300 10 10 Jill UK 5006 Fred US 300 5 5 Erik US 400 10 10 Jill UK 50010 Jill UK 5005 Erik US 400 10 10 Jill UK 5005 Erik US 400 10 10 Jill UK 50010 Jill UK 5008 Herb NL 600 10 10 Jill UK 500
}] ,
}] ,}] ,
]},
]},]},
]},]},
]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 116: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/116.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
[
1 Alex DE 300 10{ ,"bosses":[{ ,"lvl":0} }] ,10 Jill UK 500
10 Jill UK 500{ ,"bosses":[]}]
5 Erik US 400 10{ ,"bosses":[{ ,"lvl":0}10 Jill UK 500 }] ,8 Herb NL 600 10{ ,"bosses":[{ ,"lvl":0}10 Jill UK 500 }] ,}] ,
2 Bert DE 100 3 { ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,3 Cora DE 200 1 1 Alex DE 300 10 10 Jill UK 500 ]},4 Drew US 200 6 { ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,6 Fred US 300 5 5 Erik US 400 10 10 Jill UK 500 ]},
6 Fred US 300 5 { ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},5 Erik US 400 10 10 Jill UK 500 ]},7 Gina US 200 5 { ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},5 Erik US 400 10 10 Jill UK 500 ]},
3 Cora DE 200 1 { ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},1 Alex DE 300 10 10 Jill UK 500 ]},
9 Ivan NL 400 8 { ,"lvl":1}{ ,"bosses":[{ ,"lvl":0},8 Herb NL 600 10 10 Jill UK 500 ]},
The two employees bosseswith most
])
db.emps.aggregate([ {$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses" ,
{$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 117: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/117.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
"bid" "_id""$bid""bosses"
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
[ 2 Bert DE 100 3 { ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,3 Cora DE 200 1 1 Alex DE 300 10 10 Jill UK 500 ]},4 Drew US 200 6 { ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,6 Fred US 300 5 5 Erik US 400 10 10 Jill UK 500 ]}]
The two employees bosseswith most
])
db.emps.aggregate([
, {$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
![Page 118: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/118.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
[ 2 Bert DE 100 3 { ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,3 Cora DE 200 1 1 Alex DE 300 10 10 Jill UK 500 ]},4 Drew US 200 6 { ,"lvl":1} { ,"lvl":2}{ ,"bosses":[{ ,"lvl":0}, ,6 Fred US 300 5 5 Erik US 400 10 10 Jill UK 500 ]}]
The two employees bosseswith most
"bid" "_id""$bid""bosses"
![Page 119: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/119.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses.leve:-1}}, {$limit:2} "bosses.lvl"
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employees bosseswith most
"bid" "_id""$bid""bosses"
![Page 120: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/120.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses.leve:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
"bid" "_id""$bid""bosses"
The two employeesbosses with most
"bosses.lvl"
![Page 121: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/121.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses.leve:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
"bid""_id""$bid""bosses"
The two employeesbosses with most
"bosses.lvl"
![Page 122: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/122.jpg)
, {$sort:{"bosses.leve:-1}}, {$limit:2}
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employeesbosses with most
"emps""bosses.lvl"
"$_id" "_id" "bid"
![Page 123: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/123.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses.leve:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employeesbosses with most
"bosses.lvl"
"$_id" "_id" "bid""emps" ,
![Page 124: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/124.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses.leve:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"
![Page 125: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/125.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses.leve:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
![Page 126: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/126.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
![Page 127: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/127.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
$limit$graphLookup $sort$addFields
![Page 128: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/128.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
![Page 129: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/129.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
![Page 130: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/130.jpg)
[{ ,"emps":[], "hdcnt":0}]
9 Ivan NL 400 8
[{ ,"emps":[], "hdcnt":0}
7 Gina US 200 5
[{ ,"emps":[], "hdcnt":0}
4 Drew US 200 6
[{ ,"emps":[], "hdcnt":0}
2 Bert DE 100 3
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
8 Herb NL 600 10 9 Ivan NL 400 8
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
6 Fred US 300 5 4 Drew US 200 610 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
![Page 131: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/131.jpg)
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
[{ ,"emps":[], "hdcnt":0}
7 Gina US 200 5
[{ ,"emps":[], "hdcnt":0}
4 Drew US 200 6
[{ ,"emps":[], "hdcnt":0}
2 Bert DE 100 3
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
3 Cora DE 200 1 2 Bert DE 100 3
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
8 Herb NL 600 10 9 Ivan NL 400 8
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
6 Fred US 300 5 4 Drew US 200 6
[ { ,"lvl":1}],{ ,"emps":[{ ,"lvl":0}, "hdcnt":2},
1 Alex DE 300 10 3 Cora DE 200 1 2 Bert DE 100 3
, 10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
![Page 132: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/132.jpg)
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
3 Cora DE 200 1 2 Bert DE 100 3
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
8 Herb NL 600 10 9 Ivan NL 400 8
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
6 Fred US 300 5 4 Drew US 200 6
[ { ,"lvl":1}],{ ,"emps":[{ ,"lvl":0}, "hdcnt":2},
1 Alex DE 300 10 3 Cora DE 200 1 2 Bert DE 100 3
,
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
![Page 133: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/133.jpg)
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
[{ ,"emps":[{ ,"lvl":0}], "hdcnt":1},
3 Cora DE 200 1 2 Bert DE 100 3
[ { ,"lvl":1}],{ ,"emps":[{ ,"lvl":0}, "hdcnt":2},
1 Alex DE 300 10 3 Cora DE 200 1 2 Bert DE 100 3
,
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate([
])
The two employeesbosses with most
"$_id" "_id" "bid""emps" ,
"hdcnt"{$addFields: {hdcnt:{$size:"$emps"}}},
![Page 134: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/134.jpg)
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate(["$_id" "_id" "bid""emps" ,
"hdcnt" ,{$addFields: {hdcnt:{$size:"$emps"}}},
The two employeesbosses with most
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
The two employeesbosses with most
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
},
]}
![Page 135: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/135.jpg)
{$addFields: {tsal:{$reduce: { input: "$emps", initialValue: 0, in: {$add: ["$$value", "$$this.sal"]}}}}}])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate(["$_id" "_id" "bid""emps" ,
"hdcnt" ,{$addFields: {hdcnt:{$size:"$emps"}}},
The two employeesbosses with most
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
The two employeesbosses with most
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
},
]}
![Page 136: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/136.jpg)
{$addFields: {tsal:{$reduce: { input: "$emps", initialValue: 0, in: {$add: ["$$value", "$$this.sal"]}}}}}])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate(["$_id" "_id" "bid""emps" ,
"hdcnt" ,{$addFields: {hdcnt:{$size:"$emps"}}},
The two employeesbosses with most
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
The two employeesbosses with most
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
},
]}
$limit$graphLookup $sort$addFields
![Page 137: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/137.jpg)
{$addFields: {tsal:{$reduce: { input: "$emps", initialValue: 0, in: {$add: ["$$value", "$$this.sal"]}}}}}])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate(["$_id" "_id" "bid""emps" ,
"hdcnt" ,{$addFields: {hdcnt:{$size:"$emps"}}},
The two employeesbosses with most
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
The two employeesbosses with most
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
},
]}
$addFields
$reduce$limit$graphLookup $sort$addFields
![Page 138: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/138.jpg)
{$addFields: {tsal:{$reduce: { input: "$emps", initialValue: 0, in: {$add: ["$$value", "$$this.sal"]}}}}}])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate(["$_id" "_id" "bid""emps" ,
"hdcnt" ,{$addFields: {hdcnt:{$size:"$emps"}}},
The two employeesbosses with most
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
The two employeesbosses with most
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
},
]}
![Page 139: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/139.jpg)
{$addFields: {tsal:{$reduce: { input: "$emps", initialValue: 0, in: {$add: ["$$value", "$$this.sal"]}}}}}])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate(["$_id" "_id" "bid""emps" ,
"hdcnt" ,{$addFields: {hdcnt:{$size:"$emps"}}},
The two employeesbosses with most
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
The two employeesbosses with most
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
},
]}
![Page 140: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/140.jpg)
{$addFields: {tsal:{$reduce: { input: "$emps", initialValue: 0, in: {$add: ["$$value", "$$this.sal"]}}}}}])
{$graphLookup: {from:"emps", startWith:"$bid", connectFromField:"bid", connectToField:"_id", dpthField:"lvl", as:"bosses"}} , {$sort:{"bosses:-1}}, {$limit:2}
db.emps.aggregate(["$_id" "_id" "bid""emps" ,
"hdcnt" ,{$addFields: {hdcnt:{$size:"$emps"}}},
The two employeesbosses with most
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
The two employeesbosses with most
[ 10 Jill UK 500{ ,"emps":[ { ,"lvl":0} { ,"lvl":0}{ ,"lvl":0}, , ,{ ,"lvl":1} { ,"lvl":1}{ ,"lvl":1}, , ,{ ,"lvl":2} { ,"lvl":2}{ ,"lvl":1}, , 4 Drew US 200 6 ],2 Bert DE 100 3
1 Alex DE 300 10 5 Erik US 400 10 8 Herb NL 600 103 Cora DE 200 1 7 Gina US 200 5 6 Fred US 300 59 Ivan NL 400 8
"hdcnt":9},
[ 5 Erik US 400 10 { ,"lvl":0} { ,"lvl":1}{ ,"emps":[{ ,"lvl":0}, ,7 Gina US 200 5 6 Fred US 300 5 4 Drew US 200 6 ], "hdcnt":3}
},
]}
, "tsal":2700
, "tsal":700
![Page 141: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/141.jpg)
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
10 Jill UK 500_id name dpt sal bid
5 Erik US 400 10_id name dpt sal bid
7 Gina US 200 5_id name dpt sal bid
6 Fred US 300 5_id name dpt sal bid
9 Ivan NL 400 8_id name dpt sal bid
8 Herb NL 600 10_id name dpt sal bid
1 Alex DE 300 10_id name dpt sal bid
3 Cora DE 200 1_id name dpt sal bid
2 Bert DE 100 3_id name dpt sal bid
4 Drew US 200 6_id name dpt sal bid
![Page 142: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/142.jpg)
MongoDB
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
_id name dpt sal bid
![Page 143: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/143.jpg)
MongoDB
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
_id name dpt sal bidMunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
loc
![Page 144: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/144.jpg)
MongoDB
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 145: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/145.jpg)
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 146: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/146.jpg)
Chicago Boston
Austin
Orlando
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 147: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/147.jpg)
Chicago Boston
Austin
Orlando
✈
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 148: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/148.jpg)
Chicago Boston
Austin
Orlando
$
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 149: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/149.jpg)
Chicago Boston
Austin
Orlando
$1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 150: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/150.jpg)
Chicago Boston
Austin
Orlando
$
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 151: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/151.jpg)
Chicago Boston
Austin
Orlando
$
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 152: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/152.jpg)
Chicago Boston
Austin
Orlando
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
$
![Page 153: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/153.jpg)
Chicago Boston
Austin
Orlando
✈
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 154: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/154.jpg)
Chicago Boston
Austin
Orlando
✈
1 Alex DE 300 102 Bert DE 100 33 Cora DE 200 14 Drew US 200 65 Erik US 400 106 Fred US 300 57 Gina US 200 58 Herb NL 600 109 Ivan NL 400 810 Jill UK 500
MunichHamburgEssen
ChicagoBostonOrlandoAustinUtrechtArnheimLondon
_id name dpt sal bid loc
![Page 155: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/155.jpg)
Chicago Boston
Austin
Orlando
![Page 156: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/156.jpg)
Chicago Boston
Austin
Orlando
Boston 980 ChicagoBoston 2000 AustinBoston 1300 OrlandoChicago 980 BostonChicago 1150 AustinChicago 1200 OrlandoAustin 2000 BostonAustin 1150 ChicagoAustin 1100 OrlandoOrlando 1300 BostonOrlando 1200 ChicagoOrlando 1100 Austin
start enddist
![Page 157: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/157.jpg)
Orlando 1300 BostonBoston 980 ChicagoChicago 1200 OrlandoOrlando 1100 AustinAustin 2000 BostonBoston 980 ChicagoChicago 1150 AustinAustin 1100 OrlandoOrlando 1300 BostonBoston 1300 OrlandoOrlando 1100 AustinAustin 1150 ChicagoChicago 980 BostonBoston 1300 OrlandoOrlando 1200 ChicagoChicago 1150 AustinAustin 2000 BostonBoston 2000 AustinAustin 1100 OrlandoOrlando 1200 ChicagoChicago 980 BostonBoston 2000 AustinAustin 1150 ChicagoChicago 1200 Orlando
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
start enddist
Boston 980 ChicagoBoston 2000 AustinBoston 1300 OrlandoChicago 980 BostonChicago 1150 AustinChicago 1200 OrlandoAustin 2000 BostonAustin 1150 ChicagoAustin 1100 OrlandoOrlando 1300 BostonOrlando 1200 ChicagoOrlando 1100 Austin
Chicago Boston
Austin
Orlando$limit$graphLookup $sort...
![Page 158: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/158.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
5280Boston 980 Chicago Chicago 1200 Orlando Orlando 1100 Austin Austin 2000 Boston
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
4530Boston 1300 Orlando Orlando 1100 Austin Austin 1150 Chicago Chicago 980 Boston
5650Boston 1300 Orlando Orlando 1200 Chicago Chicago 1150 Austin Austin 2000 Boston
5280Boston 2000 Austin Austin 1100 Orlando Orlando 1200 Chicago Chicago 980 Boston
5650Boston 2000 Austin Austin 1150 Chicago Chicago 1200 Orlando Orlando 1300 Boston
start enddist start enddist start enddist start enddist tdist
Chicago Boston
Austin
Orlando
![Page 159: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/159.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
5280Boston 980 Chicago Chicago 1200 Orlando Orlando 1100 Austin Austin 2000 Boston
4530Boston 1300 Orlando Orlando 1100 Austin Austin 1150 Chicago Chicago 980 Boston
5650Boston 1300 Orlando Orlando 1200 Chicago Chicago 1150 Austin Austin 2000 Boston
5280Boston 2000 Austin Austin 1100 Orlando Orlando 1200 Chicago Chicago 980 Boston
5650Boston 2000 Austin Austin 1150 Chicago Chicago 1200 Orlando Orlando 1300 Boston
start enddist start enddist start enddist start enddist tdist
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
![Page 160: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/160.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
start enddist start enddist start enddist start enddist tdist
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
![Page 161: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/161.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston✈
![Page 162: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/162.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
✈
![Page 163: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/163.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
$
![Page 164: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/164.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
$
![Page 165: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/165.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
$
![Page 166: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/166.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
$
![Page 167: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/167.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
$
![Page 168: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/168.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston
✈
![Page 169: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/169.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
Chicago Boston
Austin
Orlando
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston ✈
![Page 170: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/170.jpg)
…,{$graphLookup:{from:"dists",startWith:"$end",connectFromField:"end",connectToField:"start", dpthField:"lvl", as:"routes"}}, …, {$sort:{"tdist":1}}, {$limit:1})]
4530Boston 980 Chicago Chicago 1150 Austin Austin 1100 Orlando Orlando 1300 Boston ✈
![Page 171: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/171.jpg)
![Page 172: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/172.jpg)
![Page 173: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/173.jpg)
![Page 174: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/174.jpg)
![Page 175: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/175.jpg)
![Page 176: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/176.jpg)
![Page 177: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/177.jpg)
Facets{
![Page 178: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/178.jpg)
![Page 179: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/179.jpg)
![Page 180: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/180.jpg)
MongoDB
![Page 181: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/181.jpg)
MongoDB
_id name dpt sal
![Page 182: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/182.jpg)
MongoDB
_id name dpt sal
![Page 183: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/183.jpg)
MongoDB
_id name dpt sal expertise
![Page 184: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/184.jpg)
MongoDB
_id name dpt sal expertise5 results for mongodb
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
Location
AllDENL
SalaryAll100 - 200200 - 400400 - 600
ExpertiseAllMongoDBJava
Scala
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
![Page 185: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/185.jpg)
3 results for oracle
Fredlocation: US salary: 300 expertise: Oracle, Cobol
Ginalocation: US salary: 200 expertise: Oracle, Pascal
Ivanlocation: NL salary: 400 expertise: Oracle, Fortran
Search | EmpView
Location
AllUSNL
✔
ExpertiseAllOracleCobolPascalFortran
✔
SalaryAll100 - 200200 - 400
✔
oracle
(2)(1)
(3)(1)(1)(1)
(1)(2)
https://www.awesomeempview.com/search/f?type=all&key… MongoDB
_id name dpt sal expertise
![Page 186: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/186.jpg)
MongoDB
_id name dpt sal expertise
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
![Page 187: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/187.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
_id name dpt sal expertise
![Page 188: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/188.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
_id name dpt sal expertise
db.emps.aggregate([ { $match: { $text : { $search : "mongodb" }}}, { $sortByCount: "$dpt"}])
![Page 189: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/189.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
_id name dpt sal expertise
db.emps.aggregate([ { $match: { $text : { $search : "mongodb" }}}, { $sortByCount: "$dpt"}])
![Page 190: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/190.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
{"_id":"DE", "count":3},
{"_id":"NL", "count":1},
{"_id":"US", "count":1}
_id name dpt sal expertise
db.emps.aggregate([ { $match: { $text : { $search : "mongodb" }}}, { $sortByCount: "$dpt"}])
![Page 191: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/191.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
db.emps.aggregate( [ {$match: { $text : {$search : "mongodb" }}}, {$unwind: "$expertise"}, {$sortByCount: "$expertise"}])
_id name dpt sal expertise
{"_id":"MongoDB","count":5},
{"_id":"Java","count":2},
{"_id":"Ruby","count":2},
{"_id":"Scala","count":1}
![Page 192: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/192.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
db.emps.aggregate([ {$match: { $text : {$search : "mongodb" }}}, {$bucket: { groupBy: "$sal", boundaries: [100, 201, 401, 601, Infinity]}} ])
{"_id":100,"count":2},
{"_id":201,"count":2},
{"_id":401,"count":1}
_id name dpt sal expertise
![Page 193: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/193.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
db.emps.aggregate([ {$match: { $text : {$search : "mongodb" }}}, {$bucket: { groupBy: "$sal", boundaries: [100, 201, 401, 601, Infinity], output: { count: {$sum: 1}, matches: {$push: "$$CURRENT"} }}} ])
{"_id":100,"count":2,"matches":[
]}, {"_id":201,"count":2,"matches":[
]}, {"_id":401,"count":1,"matches":[
]}
_id name dpt sal expertise
![Page 194: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/194.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
db.emps.aggregate([ {$match: { $text : {$search : "mongodb" }}}, {$bucketAuto: { groupBy: "$sal", buckets: 4}} ])
{"_id":{"min":100,"max":200}, "count":1},
{"_id":{"min":200,"max":300}, "count":1},
{"_id":{"min":300,"max":400}, “count":1},
{"_id":{"min":400,"max":600}, "count":2}
_id name dpt sal expertise
![Page 195: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/195.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
db.emps.aggregate([ {$match: { $text : {$search : {$facet: { "Location": [ {$sortByCount: "$dpt"} ], "Expertise":[ {$unwind: "$expertise"}, {$sortByCount: "$expertise"} ], "Salary":[ {$bucket: { groupBy: "$sal", boundaries: [100, 201, 401, 601, Infinity]}} ]}}])
{"Location":[ {"_id":"DE","count":3}, {"_id":"NL","count":1}, {"_id":"US","count":1}], "Expertise":[ {"_id":"MongoDB","count":5}, {"_id":"Java","count":2}, {"_id":"Ruby","count":2}, {"_id":"Scala","count":1}], "Salary":[ {"_id":100,"count":2}, {"_id":201,"count":2}, {"_id":401,"count":1}]}
_id name dpt sal expertise
}}},"mongodb"
![Page 196: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/196.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
{"Location":[ {"_id":"DE","count":3}, {"_id":"NL","count":1}, {"_id":"US","count":1}], "Expertise":[ {"_id":"MongoDB","count":5}, {"_id":"Java","count":2}, {"_id":"Ruby","count":2}, {"_id":"Scala","count":1}], "Salary":[ {"_id":100,"count":2}, {"_id":201,"count":2}, {"_id":401,"count":1}]}
_id name dpt sal expertise
db.emps.aggregate([ {$match: { $text : {$search : {$facet: { "Location": [ {$sortByCount: "$dpt"} ], "Expertise":[ {$unwind: "$expertise"}, {$sortByCount: "$expertise"} ], "Salary":[ {$bucket: { groupBy: "$sal", boundaries: [100, 201, 401, 601, Infinity]}} ]}}])
}}},"mongodb"
![Page 197: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/197.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
{"Location":[ {"_id":"DE","count":3}, {"_id":"NL","count":1}, {"_id":"US","count":1}], "Expertise":[ {"_id":"MongoDB","count":5}, {"_id":"Java","count":2}, {"_id":"Ruby","count":2}, {"_id":"Scala","count":1}], "Salary":[ {"_id":100,"count":2}, {"_id":201,"count":2}, {"_id":401,"count":1}]}
_id name dpt sal expertise
db.emps.aggregate([ {$match: { $text : {$search : {$facet: { "Location": [ {$sortByCount: "$dpt"} ], "Expertise":[ {$unwind: "$expertise"}, {$sortByCount: "$expertise"} ], "Salary":[ {$bucket: { groupBy: "$sal", boundaries: [100, 201, 401, 601, Infinity]}} ]}}])
}}},"mongodb"
![Page 198: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/198.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
{"Location":[ {"_id":"DE","count":3}, {"_id":"NL","count":1}, {"_id":"US","count":1}], "Expertise":[ {"_id":"MongoDB","count":5}, {"_id":"Java","count":2}, {"_id":"Ruby","count":2}, {"_id":"Scala","count":1}], "Salary":[ {"_id":100,"count":2}, {"_id":201,"count":2}, {"_id":401,"count":1}]}
_id name dpt sal expertise
db.emps.aggregate([ {$match: { $text : {$search : {$facet: { "Location": [ {$sortByCount: "$dpt"} ], "Expertise":[ {$unwind: "$expertise"}, {$sortByCount: "$expertise"} ], "Salary":[ {$bucket: { groupBy: "$sal", boundaries: [100, 201, 401, 601, Infinity]}} ]}}])
}}},"\"mongodb\" \"ruby\""
![Page 199: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/199.jpg)
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Bertlocation: DE salary: 100 expertise: MongoDB, Java
Coralocation: DE salary: 200 expertise: MongoDB, Java
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
Herblocation: NL salary: 600 expertise: MongoDB, Scala
LocationAllDENL
Salary
All100 - 200200 - 400400 - 600
Expertise
AllMongoDBJava
Scala
✔
(1)
(2)
(1)
(2)(2)(1)
✔
3( )
( )5
Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
5 results for mongodb
_id name dpt sal expertise
db.emps.aggregate([ {$match: { $text : {$search : {$facet: { "Location": [ {$sortByCount: "$dpt"} ], "Expertise":[ {$unwind: "$expertise"}, {$sortByCount: "$expertise"} ], "Salary":[ {$bucket: { groupBy: "$sal", boundaries: [100, 201, 401, 601, Infinity]}} ]}}])
}}},"\"mongodb\" \"ruby\""
{"Location":[ {"_id":"DE","count":1}, {"_id":"US","count":1}], "Expertise":[ {"_id":"MongoDB","count":2}, {"_id":"Ruby","count":2}], "Salary":[ {"_id":201,"count":2}]}
![Page 200: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/200.jpg)
MongoDB
Alexlocation: DE salary: 300 expertise: MongoDB, Ruby
Eriklocation: US salary: 400 expertise: MongoDB, Ruby
LocationAllDE
Salary
All200 - 400
Expertise
AllMongoDB
✔
(2)
✔
1(
( )2Ruby (2)
US (1)
✔
https://www.awesomeempview.com/search/f?type=all&key…
Search | EmpView
mongodb
2 results for mongodb
_id name dpt sal expertise
db.emps.aggregate([ {$match: { $text : {$search : {$facet: { "Location": [ {$sortByCount: "$dpt"} ], "Expertise":[ {$unwind: "$expertise"}, {$sortByCount: "$expertise"} ], "Salary":[ {$bucket: { groupBy: "$sal", boundaries: [100, 201, 401, 601, Infinity]}} ]}}])
}}},"\"mongodb\" \"ruby\""
{"Location":[ {"_id":"DE","count":1}, {"_id":"US","count":1}], "Expertise":[ {"_id":"MongoDB","count":2}, {"_id":"Ruby","count":2}], "Salary":[ {"_id":201,"count":2}]})
![Page 201: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/201.jpg)
MongoDB
![Page 202: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/202.jpg)
MongoDB
One more thing…
![Page 203: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/203.jpg)
PL Land
![Page 204: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/204.jpg)
PL Land
nats = nats. (nats. {|n| nats. (n-1) . (1) . {|p| n % p != 0} . })
droptake
mapall?
mapzip[1,2,3,4,5,6,7,8,9,10]
Ruby
![Page 205: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/205.jpg)
PL Land
[[1, true ], [2, true ], [3, true ], [4, false], [5, true ], [6, false], [7, true ], [8, false], [9, false], [10,false]]
nats = nats. (nats. {|n| nats. (n-1) . (1) . {|p| n % p != 0} . })
droptake
mapall?
mapzip[1,2,3,4,5,6,7,8,9,10]
Ruby
![Page 206: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/206.jpg)
PL Land
droptake
mapall?
mapzip[1,2,3,4,5,6,7,8,9,10]
Ruby
![Page 207: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/207.jpg)
PL Land
droptake
mapall?
mapzip$range: [1, 11, 1]
Ruby
![Page 208: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/208.jpg)
PL Land
droptake
mapall?
map$zip$range: [1, 11, 1]
Ruby
![Page 209: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/209.jpg)
PL Land
droptake
mapall?
map$zip$range: [1, 11, 1]
["A", "B", "C"][10, 20, 30]
Ruby
![Page 210: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/210.jpg)
PL Land
droptake
mapall?
map$zip$range: [1, 11, 1]
["A", "B", "C"][10, 20, 30]
[["A",10], ["B",20], ["C",30]]
Ruby
![Page 211: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/211.jpg)
PL Land
droptake
mapall?
$map$zip$range: [1, 11, 1]
Ruby
![Page 212: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/212.jpg)
PL Land
dropmapall?
$slice$map$zip$range: [1, 11, 1]
Ruby
![Page 213: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/213.jpg)
PL Land
mapall?
$slice$slice
$map$zip$range: [1, 11, 1]
Ruby
![Page 214: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/214.jpg)
PL Land
all?
$slice$slice$map
$map$zip$range: [1, 11, 1]
Ruby
![Page 215: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/215.jpg)
PL Land
$slice$slice$map
$allElementsTrue
$map$zip$range: [1, 11, 1]
Ruby
![Page 216: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/216.jpg)
PL Land
$slice$slice
$map$allElementsTrue
$map$zip
$range: [1, 11, 1]Ruby
![Page 217: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/217.jpg)
$let: {vars: {nats: { }}, in: { : {inputs: ["$$nats", { : {input: "$$nats", as: "n", in: { : [{ : {input: { : [{ : ["$$nats", {$subtract: ["$$n", 1]}]}, 1, {$size: "$$nats"}]}, as: "p", in: { $ne: [{$mod: ["$$n", "$$p"]}, 0]}}}]}}}]}}}
PL Land
$slice$slice
$map$allElementsTrue
$map$zip
: [1, 11, 1]$rangeAggregation Pipeline
![Page 218: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/218.jpg)
$let: {vars: {nats: { }}, in: { : {inputs: ["$$nats", { : {input: "$$nats", as: "n", in: { : [{ : {input: { : [{ : ["$$nats", {$subtract: ["$$n", 1]}]}, 1, {$size: "$$nats"}]}, as: "p", in: { $ne: [{$mod: ["$$n", "$$p"]}, 0]}}}]}}}]}}}
PL Land
$slice$slice
$map$allElementsTrue
$map$zip
: [1, 11, 1]$rangeAggregation Pipeline
$allElementsTrue$map$slice$slice
$map
$range $zip
![Page 219: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/219.jpg)
MongoDB
PL Land
$let: {vars: {nats: { }}, in: { : {inputs: ["$$nats", { : {input: "$$nats", as: "n", in: { : [{ : {input: { : [{ : ["$$nats", {$subtract: ["$$n", 1]}]}, 1, {$size: "$$nats"}]}, as: "p", in: { $ne: [{$mod: ["$$n", "$$p"]}, 0]}}}]}}}]}}}
$slice$slice
$map$allElementsTrue
$map$zip
: [1, 11, 1]$rangeAggregation Pipeline
$allElementsTrue$map$slice$slice
$map
$range $zip
![Page 220: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/220.jpg)
MongoDB
PL Land
[[1, true ],[2, true ],[3, true ],[4, false],[5, true ],[6, false],[7, true ],[8, false],[9, false],[10,false]]
$let: {vars: {nats: { }}, in: { : {inputs: ["$$nats", { : {input: "$$nats", as: "n", in: { : [{ : {input: { : [{ : ["$$nats", {$subtract: ["$$n", 1]}]}, 1, {$size: "$$nats"}]}, as: "p", in: { $ne: [{$mod: ["$$n", "$$p"]}, 0]}}}]}}}]}}}
$slice$slice
$map$allElementsTrue
$map$zip
: [1, 11, 1]$rangeAggregation Pipeline
$allElementsTrue$map$slice$slice
$map
$range $zip
![Page 221: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/221.jpg)
$slice$slice
$map$allElementsTrue
$map$zip
$range
![Page 222: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/222.jpg)
$slice
$map
$allElementsTrue
$zip
$range
![Page 223: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/223.jpg)
$indexStats
$stdDevSamp
$stdDevPop
$sqrt
$abs
$log
$log10$ln $pow
$exp
$trunc
$ceil
$floor$arrayElemAt $concatArrays
$isArray$filter
$avg
$min
$max
$sum$stdDevPop
$stdDevSamp
$split
$indexOf
$reduce
$reverseArray
$indexOfArray
$type
$isoWeekYear$isoDayOfWeek
$isoWeek
$in
$switch$count
$replaceRoot
$addFields
$graphLookup $facet
$bucket $bucketAuto $sortByCount$out$redact
$setEquals
$setIntersection$setUnion$setDifference
$setIsSubset $anyElementTrue$literal
$size
$geoNear $millisecond $concat
$project $match $limit$skip
$unwind $group $sort
$and $or
$not
$cmp$eq $gt
$gte
$lt$lte
$ne
$abs
$add
$ceil
$divide
$mod
$multiply
$subtract
$toLower
$toUpper
$strcasecmp$meta
$let$dayOfYear
$dayOfMonth
$dayOfWeek
$year$month
$week
$hour
$minute
$second
$millisecond
$dateToString
$cond
$ifNull
$first$last
$push
$addToSet
$indexOfBytes
$indexOfCP
$strLenBytes$strLenCP
$substrBytes$substrCP
$slice
$map
$allElementsTrue
$zip
$range
$sample$lookup
$dateToString
$collStats
![Page 224: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/224.jpg)
3.0
3.4
3.2
2.6
2.4
2.2
$indexStats
$stdDevSamp
$stdDevPop
$sqrt
$abs
$log
$log10$ln $pow
$exp
$trunc
$ceil
$floor$arrayElemAt $concatArrays
$isArray$filter
$avg
$min
$max
$sum$stdDevPop
$stdDevSamp
$out$redact
$setEquals
$setIntersection$setUnion$setDifference
$setIsSubset $anyElementTrue$literal
$size
$geoNear $millisecond $concat
$project $match $limit$skip
$unwind $group $sort
$and $or
$not
$cmp$eq $gt
$gte
$lt$lte
$ne
$abs
$add
$ceil
$divide
$mod
$multiply
$subtract
$toLower
$toUpper
$strcasecmp$meta
$let$dayOfYear
$dayOfMonth
$dayOfWeek
$year$month
$week
$hour
$minute
$second
$millisecond
$dateToString
$cond
$ifNull
$first$last
$push
$addToSet
$slice
$map
$allElementsTrue
$sample$lookup
$dateToString
$split
$indexOf
$reduce
$reverseArray
$indexOfArray
$type
$isoWeekYear$isoDayOfWeek
$isoWeek
$in
$switch$count
$replaceRoot
$addFields
$graphLookup $facet
$bucket $bucketAuto $sortByCount$indexOfBytes
$indexOfCP
$strLenBytes$strLenCP
$substrBytes$substrCP
$zip
$range
$collStats
![Page 225: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/225.jpg)
2.4 $geoNear $millisecond $concat
2.2
$avg
$min
$max
$sum$stdDevPop
$stdDevSamp
$project $match $limit$skip
$unwind $group $sort
$and $or
$not
$cmp$eq $gt
$gte
$lt$lte
$ne
$abs
$add
$ceil
$divide
$mod
$multiply
$subtract
$toLower
$toUpper
$strcasecmp$meta
$let$dayOfYear
$dayOfMonth
$dayOfWeek
$year$month
$week
$hour
$minute
$second
$millisecond
$dateToString
$cond
$ifNull
$first$last
$push
$addToSet
$map
2.6 $out$redact
$setEquals
$setIntersection$setUnion$setDifference
$setIsSubset $anyElementTrue$literal
$size
$allElementsTrue
3.2$indexStats
$stdDevSamp
$stdDevPop
$sqrt
$abs
$log
$log10$ln $pow
$exp
$trunc
$ceil
$floor$arrayElemAt $concatArrays
$isArray$filter
$slice
$sample$lookup
3.0$dateToString
3.4
$indexOf $indexOfArray
$type
$isoWeekYear$in
$switch$count
$replaceRoot
$addFields
$graphLookup $facet
$bucket $bucketAuto $sortByCount$indexOfBytes
$indexOfCP$substrBytes$substrCP
$zip
$range
$split
$reduce
$reverseArray $isoWeekYear$isoDayOfWeek
$isoWeek
$strLenBytes$strLenCP $collStats
Data-Parallel Pipeline Execution
Shard 1 Shard 2 Shard 3 Shard N
mongos
Q
PipelineOptimisations
Pipeline Projection Optimization
Pipeline Coalescence Optimization
. . .
Pipeline Reordering
Pipeline Combining
![Page 226: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/226.jpg)
2.4 $geoNear $millisecond $concat
2.2
$avg
$min
$max
$sum$stdDevPop
$stdDevSamp
$project $match $limit$skip
$unwind $group $sort
$and $or
$not
$cmp$eq $gt
$gte
$lt$lte
$ne
$abs
$add
$ceil
$divide
$mod
$multiply
$subtract
$toLower
$toUpper
$strcasecmp$meta
$let$dayOfYear
$dayOfMonth
$dayOfWeek
$year$month
$week
$hour
$minute
$second
$millisecond
$dateToString
$cond
$ifNull
$first$last
$push
$addToSet
$map
2.6 $out$redact
$setEquals
$setIntersection$setUnion$setDifference
$setIsSubset $anyElementTrue$literal
$size
$allElementsTrue
3.2$indexStats
$stdDevSamp
$stdDevPop
$sqrt
$abs
$log
$log10$ln $pow
$exp
$trunc
$ceil
$floor$arrayElemAt $concatArrays
$isArray$filter
$slice
$sample$lookup
3.0$dateToString
3.4
$indexOf $indexOfArray
$type
$isoWeekYear$in
$switch$count
$replaceRoot
$addFields
$graphLookup $facet
$bucket $bucketAuto $sortByCount$indexOfBytes
$indexOfCP$substrBytes$substrCP
$zip
$range
$split
$reduce
$reverseArray $isoWeekYear$isoDayOfWeek
$isoWeek
$strLenBytes$strLenCP $collStats
![Page 227: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/227.jpg)
Aggregation Pipelines
(1) are one of the easiest ways to work with your data.
(2) are highly scalable and optimised for performance.
(3) are (much) more capable and powerful than you think!
![Page 228: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/228.jpg)
Use the Force Aggregation Pipelines!
![Page 229: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/229.jpg)
Use the Force Aggregation Pipelines! And take control over your Data.
![Page 230: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/230.jpg)
![Page 231: Advanced MongoDB Aggregation Pipelines](https://reader037.fdocuments.in/reader037/viewer/2022110109/5a6cf3137f8b9ae5418b4771/html5/thumbnails/231.jpg)
ASK THE EXPERTSGet your technical questions answered
By appointment only – register in one of two ways:
In person Online
Come to the MongoDB Team Stand on the ground floor
calendly.com/mongodb