Commit dea3fc59 authored by Marcel Huber's avatar Marcel Huber
Browse files

WIP: mongodb solutions for $lookup added

parent 6ed4ce4b
......@@ -297,7 +297,7 @@ Weiterführende Informationen zu diesen Aufgaben finden Sie in https://github.co
Verwenden Sie für diese Übung eine neue Datenbank `uebung2`.
.Basisdaten
.Basisdaten der Collection `unicorns`
[source,javascript]
----
{_id: ObjectId("5cc6e5d94249f49a59ac4000"), name: "Horny", dob: new Date(1992,2,13,7,47), loves: ["carrot","papaya"], weight: 600, gender: "m", vampires: 63},
......@@ -735,15 +735,14 @@ In dieser Aufgabe sollen Sie die verschiedenen Varianten kennenlernen.
Weiterführende Informationen zu dieser Aufgabe finden Sie auch in https://github.com/karlseguin/the-little-mongodb-book/blob/master/en/mongodb.markdown#chapter-4---data-modeling[Data Modeling] von The Little MongoDB Book.
.Daten
.Daten der Collection `employees`
[source,javascript]
----
db.employees.insertMany([
{_id: ObjectId("4d85c7039ab0fd70a117d730"), name: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d731"), name: "Duncan", manager: ObjectId("4d85c7039ab0fd70a117d730")},
{_id: ObjectId("4d85c7039ab0fd70a117d732"), name: "Moneo", manager: ObjectId("4d85c7039ab0fd70a117d730")},
{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: "Siona", manager: [ObjectId("4d85c7039ab0fd70a117d730"), ObjectId("4d85c7039ab0fd70a117d732")]},
{_id: ObjectId("4d85c7039ab0fd70a117d734"), name: "Ghanima", family: {mother: "Chani", father: "Paul", brother: ObjectId("4d85c7039ab0fd70a117d730")}}
{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: "Siona", manager: [ObjectId("4d85c7039ab0fd70a117d730"), ObjectId("4d85c7039ab0fd70a117d732")]}
])
----
......@@ -791,7 +790,7 @@ endif::blubby[]
In dieser Aufgabe verwenden wir die https://docs.mongodb.com/manual/aggregation/[Aggregation Pipeline] und den https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/[`$graphLookup`] Operator.
.Daten
.Daten der Collection `employees`
[source,javascript]
----
{_id: ObjectId("4d85c7039ab0fd70a117d730"), name: "Leto"},
......@@ -830,7 +829,10 @@ db.employees.aggregate([
{ $match: { "name":"Moneo"} },
{ $project: { _id:0, name:1, manager:1, managerDocs:1 } }
])
----
.Resultat der Abfrage
[source, javascript]
----
{ "name" : "Moneo", "manager" : "Leto", "managerDocs" : [ { "_id" : ObjectId("4d85c7039ab0fd70a117d730"), "name" : "Leto" } ] }
----
=====
......@@ -841,10 +843,83 @@ endif::exercise_solution[]
Finden Sie eine Möglichkeit einen Join mittels https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/[`$lookup`] Operator durchzuführen?
Welche Daten passen dazu besser, die von <<mongodb-joins-manual>> oder von <<mongodb-joins-graphlookup>>
.[#lookup-manager-moneo-which-data]#Q{counter:query_number} Passendes DataSet#
Welche Daten passen besser zur `$lookup` Abfrage, die von <<mongodb-joins-manual>> oder von <<mongodb-joins-graphlookup>>?
ifndef::exercise_solution[]
[example,title=""]
=====
{nbsp}
{nbsp}
{nbsp}
=====
endif::exercise_solution[]
ifdef::exercise_solution[]
[example,title=""]
=====
Die Daten der Aufgabe <<mongodb-joins-graphlookup>> passen gut für diese einfache Join Abfrage.
=====
.[#lookup-manager-moneo]#Q{counter:query_number} Manager von *Moneo* ermitteln#
Erstellen Sie eine Abfrage um den Manager von *Moneo* anzuzeigen unter Verwendung von `localField` und `foreignField`.
.Daten der Collection `employees`
[source,javascript]
----
{_id: ObjectId("4d85c7039ab0fd70a117d730"), name: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d731"), name: "Duncan", manager: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d732"), name: "Moneo", manager: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: "Siona", manager: ["Leto", "Moneo"] }
----
.Manager von *Moneo* mit einfachem `$lookup` Aggregat
[source,javascript]
----
db.employees.aggregate({
$lookup: {
from: "employees",
localField: "manager",
foreignField: "name",
as: "managers"
}
}, {
$match: {
"name": "Moneo"
}
}, {
$project: {
_id: 0,
manager: 0,
managers: {
_id: 0,
manager: 0
}
}
})
----
.Resultat der Abfrage
[source, javascript]
----
{ "name" : "Moneo", "managers" : [ { "name" : "Leto" } ] }
----
.[#lookup-manager-moneo]#Q{counter:query_number} Manager von *Moneo*#
Erstellen Sie eine Abfrage um den Manager von *Moneo* anzuzeigen.
Erstellen Sie eine Abfrage um den Manager von *Moneo* anzuzeigen wenn die Daten in zwei unterschiedlichen Collections abgelegt sind.
.Daten
[source,javascript]
----
db.employees.insertMany([
{_id: ObjectId("4d85c7039ab0fd70a117d731"), name: "Duncan", manager: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d732"), name: "Moneo", manager: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: "Siona", manager: ["Leto", "Moneo"]}
])
db.managers.insertMany([
{_id: ObjectId("4d85c7039ab0fd70a117d740"), name: "Leto", department: "Sales"},
{_id: ObjectId("4d85c7039ab0fd70a117d741"), name: "Moneo", department: "Research"}
])
----
ifndef::exercise_solution[]
[example,title=""]
=====
......@@ -859,9 +934,35 @@ ifdef::exercise_solution[]
[example,title=""]
=====
.Manager von *Moneo* mit `$lookup`
[source, subs="attributes", javascript]
[source, javascript]
----
db.employees.aggregate({
$lookup: {
from: "managers",
localField: "manager",
foreignField: "name",
as: "managers"
}
}, {
$match: {
"name": "Moneo"
}
}, {
$project: {
_id: 0,
manager: 0,
managers: {
_id: 0
}
}
})
----
.Resultat der Abfrage
[source, javascript]
----
tbd.
{ "name" : "Duncan", "managers" : [ { "name" : "Leto", "department" : "Sales" } ] }
{ "name" : "Moneo", "managers" : [ { "name" : "Leto", "department" : "Sales" } ] }
{ "name" : "Siona", "managers" : [ { "name" : "Leto", "department" : "Sales" }, { "name" : "Moneo", "department" : "Research" } ] }
----
=====
endif::exercise_solution[]
......
......@@ -297,7 +297,7 @@ Weiterführende Informationen zu diesen Aufgaben finden Sie in https://github.co
Verwenden Sie für diese Übung eine neue Datenbank `uebung2`.
.Basisdaten
.Basisdaten der Collection `unicorns`
[source,javascript]
----
{_id: ObjectId("5cc6e5d94249f49a59ac4000"), name: "Horny", dob: new Date(1992,2,13,7,47), loves: ["carrot","papaya"], weight: 600, gender: "m", vampires: 63},
......@@ -735,15 +735,14 @@ In dieser Aufgabe sollen Sie die verschiedenen Varianten kennenlernen.
Weiterführende Informationen zu dieser Aufgabe finden Sie auch in https://github.com/karlseguin/the-little-mongodb-book/blob/master/en/mongodb.markdown#chapter-4---data-modeling[Data Modeling] von The Little MongoDB Book.
.Daten
.Daten der Collection `employees`
[source,javascript]
----
db.employees.insertMany([
{_id: ObjectId("4d85c7039ab0fd70a117d730"), name: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d731"), name: "Duncan", manager: ObjectId("4d85c7039ab0fd70a117d730")},
{_id: ObjectId("4d85c7039ab0fd70a117d732"), name: "Moneo", manager: ObjectId("4d85c7039ab0fd70a117d730")},
{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: "Siona", manager: [ObjectId("4d85c7039ab0fd70a117d730"), ObjectId("4d85c7039ab0fd70a117d732")]},
{_id: ObjectId("4d85c7039ab0fd70a117d734"), name: "Ghanima", family: {mother: "Chani", father: "Paul", brother: ObjectId("4d85c7039ab0fd70a117d730")}}
{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: "Siona", manager: [ObjectId("4d85c7039ab0fd70a117d730"), ObjectId("4d85c7039ab0fd70a117d732")]}
])
----
......@@ -791,7 +790,7 @@ endif::blubby[]
In dieser Aufgabe verwenden wir die https://docs.mongodb.com/manual/aggregation/[Aggregation Pipeline] und den https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/[`$graphLookup`] Operator.
.Daten
.Daten der Collection `employees`
[source,javascript]
----
{_id: ObjectId("4d85c7039ab0fd70a117d730"), name: "Leto"},
......@@ -830,7 +829,10 @@ db.employees.aggregate([
{ $match: { "name":"Moneo"} },
{ $project: { _id:0, name:1, manager:1, managerDocs:1 } }
])
----
.Resultat der Abfrage
[source, javascript]
----
{ "name" : "Moneo", "manager" : "Leto", "managerDocs" : [ { "_id" : ObjectId("4d85c7039ab0fd70a117d730"), "name" : "Leto" } ] }
----
=====
......@@ -841,10 +843,83 @@ endif::exercise_solution[]
Finden Sie eine Möglichkeit einen Join mittels https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/[`$lookup`] Operator durchzuführen?
Welche Daten passen dazu besser, die von <<mongodb-joins-manual>> oder von <<mongodb-joins-graphlookup>>
.[#lookup-manager-moneo-which-data]#Q{counter:query_number} Passendes DataSet#
Welche Daten passen besser zur `$lookup` Abfrage, die von <<mongodb-joins-manual>> oder von <<mongodb-joins-graphlookup>>?
ifndef::exercise_solution[]
[example,title=""]
=====
{nbsp}
{nbsp}
{nbsp}
=====
endif::exercise_solution[]
ifdef::exercise_solution[]
[example,title=""]
=====
Die Daten der Aufgabe <<mongodb-joins-graphlookup>> passen gut für diese einfache Join Abfrage.
=====
.[#lookup-manager-moneo]#Q{counter:query_number} Manager von *Moneo* ermitteln#
Erstellen Sie eine Abfrage um den Manager von *Moneo* anzuzeigen unter Verwendung von `localField` und `foreignField`.
.Daten der Collection `employees`
[source,javascript]
----
{_id: ObjectId("4d85c7039ab0fd70a117d730"), name: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d731"), name: "Duncan", manager: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d732"), name: "Moneo", manager: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: "Siona", manager: ["Leto", "Moneo"] }
----
.Manager von *Moneo* mit einfachem `$lookup` Aggregat
[source,javascript]
----
db.employees.aggregate({
$lookup: {
from: "employees",
localField: "manager",
foreignField: "name",
as: "managers"
}
}, {
$match: {
"name": "Moneo"
}
}, {
$project: {
_id: 0,
manager: 0,
managers: {
_id: 0,
manager: 0
}
}
})
----
.Resultat der Abfrage
[source, javascript]
----
{ "name" : "Moneo", "managers" : [ { "name" : "Leto" } ] }
----
.[#lookup-manager-moneo]#Q{counter:query_number} Manager von *Moneo*#
Erstellen Sie eine Abfrage um den Manager von *Moneo* anzuzeigen.
Erstellen Sie eine Abfrage um den Manager von *Moneo* anzuzeigen wenn die Daten in zwei unterschiedlichen Collections abgelegt sind.
.Daten
[source,javascript]
----
db.employees.insertMany([
{_id: ObjectId("4d85c7039ab0fd70a117d731"), name: "Duncan", manager: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d732"), name: "Moneo", manager: "Leto"},
{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: "Siona", manager: ["Leto", "Moneo"]}
])
db.managers.insertMany([
{_id: ObjectId("4d85c7039ab0fd70a117d740"), name: "Leto", department: "Sales"},
{_id: ObjectId("4d85c7039ab0fd70a117d741"), name: "Moneo", department: "Research"}
])
----
ifndef::exercise_solution[]
[example,title=""]
=====
......@@ -859,9 +934,35 @@ ifdef::exercise_solution[]
[example,title=""]
=====
.Manager von *Moneo* mit `$lookup`
[source, subs="attributes", javascript]
[source, javascript]
----
db.employees.aggregate({
$lookup: {
from: "managers",
localField: "manager",
foreignField: "name",
as: "managers"
}
}, {
$match: {
"name": "Moneo"
}
}, {
$project: {
_id: 0,
manager: 0,
managers: {
_id: 0
}
}
})
----
.Resultat der Abfrage
[source, javascript]
----
tbd.
{ "name" : "Duncan", "managers" : [ { "name" : "Leto", "department" : "Sales" } ] }
{ "name" : "Moneo", "managers" : [ { "name" : "Leto", "department" : "Sales" } ] }
{ "name" : "Siona", "managers" : [ { "name" : "Leto", "department" : "Sales" }, { "name" : "Moneo", "department" : "Research" } ] }
----
=====
endif::exercise_solution[]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment