Here's example number one, we had a kind of related example earlier in this course already. Let's
say we're creating our database, our application for a hospital and there, we got patients and there, disease
summary.
So every patient has one disease summary which belongs to that patient only
and there is only one summary per patient.
So we're not talking about the history we had earlier in this course,
it's just a summary,
something like is ill, had previous diseases and a list of previous diseases, things like that.
So one summary per patient
and of course this is a one-to-one relation because the summary of patient A can never belong to patient
B
and the other way around, how could we model that? Back in a brand new cleaned up database server, I'll
switch to my hospital database which will be created on the fly
and there, I want to create my patients collection or to be precise, I want to insert a patient, so let's insert
one patient.
Now let's say a patient simply has a name Max, an age and then we have that diseaseSummary,
now let's go for the reference approach first, diseaseSummary could be summary-max-1, any identifier
you want,
so here I'll go for that ID approach.
We can insert that
and obviously if I now inspect my patients with findOne pretty, we see that one patient in there, without
pretty though, that is not existing there,
so we see the one patient in there.
Now let's also create another collection, diseaseSummaries and then there I'll also insert one summary,
there I'll explicitly define the ID and use my own ID, summary-max-1, I could have also gone with the
automatically generated objectId
but then I should have generated the summaries collection in that entry first so that I could take
that objectId and use it as an ID on the patient.
I did it the other way around,
so I'm using my own ID here
and there I'll simply add my diseases array which simply lists all diseases
I ever had and let's say that's a cold and that's a broken leg which I never had
but anyways. So now I added this too and now of course I can access my diseaseSummary, summaries collection here,
find the one element that is in there and this is the element I just inserted with my own ID and
that ID of course matches the ID I stored as a reference on my patient.
Now let's say in our application, the website we're building, the mobile app, whatever it is,
we now have a request where we need that patient and also the disease history or the disease summary I should
say.
So we might have an incoming request which in the end by the mongodb driver is translated to a
find request,
so we first of all find our patients and in reality, we'll probably do that by looking for let's say
all patients with a name of Max or here as a shortcut since I only have one, I'll just use findOne,
the thing is in the end we have some query that returns Max here.
So that is my returned
patient data,
the problem is I'm also interested in the diseaseSummary,
so I also want to get that.
Now what I can of course do is in my application, I can access diseaseSummary and therefore get my key
here and in the application we're writing, in PHP, .NET, Java, NodeJS, whatever it is, we can of course
fetch some data from a server and then store that data in a variable,
so let's say here in the shell, we can do this with the var keyword, we have our dsid for diseaseSummary
ID and that simply is the result of patients findOne and then there, accessing the diseaseSummary,
so now if we output dsid, that is the ID stored in this patient. And now we can simply reach out to
diseaseSummaries, so that other collection
and there we want to find the first diseaseSummary where _id is equal to dsid,
so I can use that variable here
and I simply stored it in a variable so that I don't have to paste or add this long term here as a value
for ID.
If I do that, I indeed find my diseaseSummary,
the bad thing here is that this took two steps
and also from a performance perspective, this will actually be super fast
but if you have a very big database, a very big collection of patients and a big collection of
diseaseSummaries,
this might not be the optimal way of doing that, splitting that across two collections feels a bit unnecessary
here,
the better approach in such a case where we have a strong one-to-one relation would be to use embedded
document.
so let me actually reach out to my patients and delete all patients in there
and now let's insert that patient again,
so that insert one command from earlier where I inserted Max,
now I will not insert a diseaseSummary which is a reference but instead I'll insert a nested, an embedded
document here and now that embedded document would have my diseases key, my diseases field with the
cold and the broken leg, like this.
And now the huge advantage of course is that in the application we're writing with whichever language
we use, I can simply write my query to find my patient by ID, by name or in this case as a shortcut, simply
with findOne and I will get all the data I need,
the patient data and the diseaseSummary data.
So this is an example for when you would use an embed document, you have a strong one-to-one relationship.