Now that we talked so much about relations,
let me show you one useful operation mongodb offers for merging related documents that
you split up by using the reference approach,
so where you got no embedded documents and that is the lookup
operator. Now the lookup operator is used like this,
this uses the aggregate method which we haven't learned about,
we got a whole module about the aggregation framework and I don't want to talk too much about it
but lookup is essentially a helpful tool that allows you to fetch two related documents merged together
in one document. in one step instead of having to do two steps
and this mitigates some of the disadvantages of splitting your documents across collections because
now you can at least merge them in one go.
Let me show you how it works.
Here we got our book author example still
and now let's say I want to fetch all my books with the author data included, to do that
I can use my books, my books collection and use the aggregate method,
again this uses the so-called aggregation framework
and we'll have a look at this later in the course in detail.
There you pass an array because you can define multiple steps on how to aggregate your data,
for now there is only one step I'm interested,
a step is simply a document you pass to the array that is the lookup step, $lookup. That
step is configured by passing a document as a value
and here you need to define four things, I simply brought this into a new page to make it easier to read,
so four things need to be defined here.
The first one is the from thing
and the question here is from which other collection do you want to relate documents?
So here we're running aggregate on the books collection,
so we want to pull in other documents from the authors collection,
so that is the name of the collection where your related documents live in. You then define the local field,
so in the collection we're running this on, so the books collection, where can the references to the other
collection be found in? And for books, we stored that in the authors field,
authors simply is an array of objectIds.
Now we need to tell MongoDB ok I know where your keys are stored,
which field are you relating to in your target collection,
so in the author collection here, the authors collection and you define that with the foreign field key
and there we know it's an _ID.
Please note that all these values are enclosed in double quotation marks, authors, authors
and here, my _ID field.
So I know that in authors, in books I have objectId stored where the matching equivalence can be found
in _ID in the authors collection.
Last but not least, you give this an alias under which this will be merged and I'll name this creators, the
name is up to you here.
If you hit enter, let's pretty print this,
we get back this result. What we see here is we get our book, actually all the books we have in there,
only one in our case with the name with the authors and the objectIDs
but then a new key was added,
creators
and that actually holds the author data which lived in a different collection and which was merged
into a result by the aggregate
and the lookup function.
So this is very useful for merging data in one step and it allows you to get the best of both worlds,
having it split up and still fetching it in one go.
Now this still is not an excuse for always using references because obviously this costs more performance
than having an embedded document,
so if you can and if your application needs it, go for an embedded document.
If you have to use references or if you want to use references, well then this lookup step in the aggregate
method can at least help you get the data you need.
Now it was just a first look at aggregate, as I mentioned we'll have a whole module where we see what
else aggregate can do for us.
Be aware of
lookup for merging your relations though.