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
In this video, let's continue to translate
the data model that we established right at the beginning
of the section into some actual code.
And so, this time we're gonna implement the reviews model.
So the first step is to create a new file
in the models folder and call it ReviewModel.js.
And to me, this looks like yet another great challenge
for you to practice some of the skills
that you already learned up to this point.
Okay so, in this challenge, I want you to create
the review model.
And what we need in there is actually very simple.
So let me put that here in a comment.
So we basically want a review,
so the text for the review.
We want a rating
so just like this.
Then we also want a createdAt
so with the current timestamp
and then we also want a reference to the tour
that this review belongs to
and also to the user who wrote this review.
Okay, so basically two parent references here, right.
And so with the stuff that you learned
in the last couple of lectures,
you should be able to actually complete this challenge
completely on your own.
So, pause the video now and good luck with that.
Now, okay, so I hope you didn't just skip this
and instead, you really implemented it by yourself
and anyway, this is how I created it.
So of course, we need to start by requiring mongoose
and then
the reviewSchema is equal to new mongoose.Schema.
And so let's start adding our fields.
So first the review is of course a string.
Now, right and of course, I'm doing this too fast,
so I also need to specify type
and then let's also set it to a required,
true,
review can not be empty.
Then we want a rating,
and this one is of the type number.
And the rating is ideally between one and five.
And so the minimum of one and the max is five.
So as you already know,
these are some simple validators.
That work only for numbers, right.
This is not correct here.
Okay.
Next up, the createdAt.
Into this is of type, date,
and by default we want
Date.now.
Okay.
So this was the easy part.
And lets actually also create a model
and export it before adding these references.
So const,
Review is equal too mongoose.model
with the name Review.
And then of course reviewSchema.
To finish of course module.export
equals the Review.
Alright, and now comes the reference part.
So a review of course needs to belong to a tour.
And it also needs an author.
Okay.
So that's again what we specified
in our data modeling lecture.
So basically that we were going to implement
parent referencing here in this case.
Because both the tour and the user
are in a sense the parents of this data set.
And we decided to do it this way
because we are going to potentially
huge erase any parent elements right?
So we should not design our application
thinking that there will only be a few reviews.
Only to then come back to it after some time
and find out that our assumptions were wrong.
And now we need to rebuild our entire data model.
So that would be terrible.
So in many situations, as you already know
what we do not really know, how much our erase will grow.
Then it's just best to opt for parent referencing.
So again, that's exactly what we're doing here
when we're referencing tour and user.
So lets actually now go ahead and implement that.
And starting with the tour.
Alright.
So here we need some space, and okay.
So the type is as we did before, mongoose,
and that's not correct.
So mongoose.Schema.ObjectId,
with a capital O and a capital I, but not the D.
And I'm just telling you that
because I once wrote ID like this,
and then spent half an hour trying to figure out
why my application wasn't working.
Okay, so this D needs to be lowercase.
Anyway.
The reference in this case is of course the tour.
And we also want to make this required.
So true and then Review must belong
to a tour.
Okay.
And just like this, each Review document now knows
exactly what tour it belongs too.
While the tour of course doesn't know initially
what Reviews and how many Reviews there are.
But that it a problem that
we will actually solve a bit later.
Okay.
Next up, when there is a Review, we not only
want to know what tour it belongs to,
but also who wrote this Review.
So user,
and again, the same type of
mongoose.Schema.ObjectId,
and this time
the reference, as you can imagine is gonna be user.
Okay, and again we make it required
because well, a review really cannot work
without an author basically.
And so review
must belong to a user.
Okay, that's actually it.
Alright.
Now just to finish lets actually also add these options
to the Schema where we make it so that virtual properties
also show up in Json and object outputs.
Okay.
So we did that before in the tours.
I'm not sure if we did it in the user model.
But anyway, here we are actually
gonna need it a bit later.
So we need to specify that.
Remember, basically what I mean
is this part.
So I'm actually going to go ahead and copy it.
Just to save us some time.
And so again, all this does is to really make sure
that when we have a virtual property,
basically a field that is not stored in the database
but calculated using some other value.
So we want this to also show up whenever there is an output.
Right, and so that's what we do here.
Okay.
And so I actually think that's actually it for now.
Very simple.
The only a bit tricky part is actually to create
these references here.
But we actually did that before
and so it really works in the exact same way.
So in the next video we will then use the Schema
in order to create some new reviews.
So see you then.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.