Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So we embedded users
into tours in the last video,
and also talked about the drawbacks
of that approach in our specific situation.
And so in this video let's actually connect tours
and users not by embedding but instead by a reference.
And so to start, let's actually get rid
of the code we wrote in the last lecture.
Okay, so this one here,
responsible for performing the embedding basically.
And now let's go here to our guides field.
Okay?
So this time in this video,
the idea is that tours and users will always remain
completely separate entities in our database.
Okay?
So all we save on a certain tour document
is the IDs of the users that are the tour guides
for that specific tour.
Then when we query the tour,
we want to automatically get access to the tour guides.
But again, without them being actually
saved on the tour document itself.
And that exactly is referencing.
So just as we talked about by the beginning of this section.
All right?
And so let me now show you
how we can implement referencing using Mongoose.
Okay?
So, here in the guides we will now want to specify an array.
Okay?
So just like we did before with the locations.
And so that then again means
that these will be some sub-documents.
So embedded documents.
All right?
And now the type is going to be
a new type that we never saw before.
And that is mongoose.Schema.objectId.
Okay?
And what this means is that we expect
a type of each of the elements
in the guides array to be a MongoDB ID.
All right?
And here actually it needs to be a capital O,
okay, otherwise it's not gonna work,
and also all of this here needs to be inside of an object,
just like any other schema type definition.
Okay, because that's all this really is.
Okay, so the type is of this MongoDB ID, basically,
and then we also need to now specify the reference.
And this is where the magic happens behind the scenes,
because here, now we say that the reference should be user.
Okay, and so this really is how we establish references
between different data sets in Mongoose.
And for this we actually do not even need
to have the user to be imported into this document.
So we actually had that here, but we don't even need it.
We had that here for the previous lecture,
but we can comment it out,
because what we just did here is still gonna work.
Okay?
So, let's now go ahead and create a new tour,
and actually I'm going to do it just the way we did before.
So let's just go ahead and delete this one.
Where is the ID?
Here we go.
So delete tour.
So we can use our API for that already.
All right, and now we create a new tour.
And so just like before,
all we pass into the guides is an array of the IDs.
All right?
But this time we actually specified
that an object ID is exactly what we expect.
Right?
So, this here is of the type object ID.
But behind the scenes, it's also referenced to the user.
All right?
So, when we now create this tour here,
it will actually only contain these IDs,
and not the user corresponding to the IDs.
So let's take a look at that.
And so yeah, here we go.
So this is exactly the data that we put in.
Just the IDs of the tour guides.
Okay, and that's exactly what we were expecting.
Because for now, all we really have
is the references inside Mongoose.
And we will then take care of actually getting the data
to show up in our output in the next video.
Okay, because we will do that in two different ways.
Let's also take a look here at Compass really quick,
just to see that as well where is that, yeah, here.
So you see object ID actually,
and then of course, the IDs that we passed in.
So just as we expected with tours and users,
still two completely different entities.
And as I said before, in the next video,
we will then take care of actually displaying
the user data in the output,
using a process called populating.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.