All language subtitles for 005 Creating Documents and Testing the Model_Downloadly.ir_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Let's now start creating documents,

testing the model, and really start using Mongoose.

And I'm really excited about this video,

so really let's get started here.

So what we're gonna do is to create a new variable,

and I'm gonna call it testTour,

and this will be a new document

created out of the tour model

that we created in the last lecture.

So what we're gonna do

is to say new Tour,

and then in there pass an object with the data.

So name, let's say,

again, 'The Forest Hiker,'

just as we've been using before,

then the rating, let's say 4.7,

and then the price,

let's say 497, okay?

So, this is, again, a new document

that we created out of a tour model,

or function constructors,

and so as I said before, this is kinda

using JavaScript function constructors,

or JavaScript classes if you're using ES6,

basically to create new objects

out of a class, right,

and so that's exactly the same syntax, actually,

so we use the name keyword,

and then the class name in ES6,

just like this, all right?

Now here, it's not exactly the same,

but this analogy is helpful

to understand how this works.

So this test tour document that we just created

is an instance of the tour model,

and so now it has a couple of methods on it

that we can use in order to

interact with the database.

So let me show that to you.

So we can now say testTour.save,

and this will then save it to

the tours collection in the database, okay,

and it's really as simple as that.

So again, we have our document instance

which is testTour, and on there

we can then call the save method

in order to save the document to the database.

Now this save here will then return

a promise that we can then consume.

So for now, let's use then for that,

and in the future, we will actually use

async await in order to consume these promises,

but for now, let's keep it simple

and not create any more functions,

and so instead, we're just using

the then method here.

So in here, we get access to the document

that was just saved to the database.

So, basically, the result value of the promise

that the save method returns is

the final document as it is in the database.

And so let's now actually go ahead

and log the document to the console, all right?

Now, on the other hand, saving this document

to the database might also go wrong,

and so let's catch that error, okay?

So just like before, we used the catch

method in order to catch any potential errors,

and so if there is an error when

saving the document to the database,

we will be able to catch that error

here in this method.

Okay, so basically, here in this function

that I'm passing into catch.

Okay, for now, all I wanna do, really,

is to also log it to the console.

Let's say, 'Error,' add some emojis here

to make it more visible in the console,

and then we can pass in a second argument

into the console log, which will

then be the error, okay?

Are you ready?

Then, let's give it a save now,

and that will then execute the code that is in here,

so basically create this new tour, and try to save it

to the database right away, okay?

So, let's give it a save now,

and see what happens in the console,

and indeed, here we go.

So you see, that this is the document

that was just saved to our database, all right?

So here we actually see the ID as well,

and that is simply auto-generated by MongoDB,

and it's simply the unique identifier for the document.

So just like it happened before in the MongoDB section,

we automatically get this unique identifier

for the new object.

Then Mongoose also automatically adds

this V, and set it to zero here,

but that's not really important at this point, okay?

What matters here is that our new document

was actually successfully added to the database,

and just to make sure, let's actually move over

to Compass and take a look at our database there.

So here, we have the tours collection,

and then we have the document that we just created.

So it has that exact same ID, and of course

all the other data that we put in there,

and so indeed, this is the object

that we just created.

And keep in mind that we had actually deleted

the tours collection here in one

of the previous videos, right,

but Mongoose automatically created

this new collection here as soon

as we created the first document

using the tour model,

and so this name here basically

comes from that tour model, it simply

gives us this plural name, just as we had before.

Perfect, we just made our express application

really interact with a MongoDB database

for the very first time,

and that's actually really amazing, if you ask me.

So this means that right now,

we are able to create documents right from our code.

So, that makes me really happy,

and so let's actually try it again

with another one, all right,

but actually before we do that,

I want to just reload this file here

which will then try to save this document here

to the database again, right,

simply because all this code here is run

each time that we save the file.

Now, let's try to guess what is gonna happen

as soon as I save this file.

Can you guess?

Let's wait for it,

and indeed we get something different now,

we get an error here,

and the error that we see is this

duplicate key error collection,

which down here says this duplicate key, okay?

So we already have a tour with the name

of 'Forest Hiker,' and so now we were

trying to create another one,

and since in our schema we have this

unique property here, that is not allowed,

remember that?

So we set this unique here to true

in order to make it impossible

to have two tours with the same name,

and so we just proved that this is actually working.

All right, so let's simple replace

the code that we have here

in order to create a new tour with another name.

Let's call it 'The Park Camper,'

and let's go ahead and remove the price

and also the rating.

So, can you guess what's gonna happen now?

Let's actually clear, or actually it

already loaded it because as soon

as I clicked here, it then saved the file,

and so, yeah we just got another error,

so you can see that up here,

meaning that we didn't successfully save

this document to the database,

and what we got here now is a validation error

coming from the price field

saying 'A Tour must have a price,'

and so that is exactly the message

that we specified up here, right?

Simply because in this line we said

that the price is a required field, all right,

and so this required, as I mentioned,

is a validator, and so down here,

we then get a validation error.

And so let's now fix that

and specify the price, let's say 997,

and give it another save.

Now this time, it should work

because the rating is not required,

and so when we come down here,

our document was actually successfully

saved to the database, you see?

Now, we didn't specify the rating here

in this test tour, but you see it's still 4.5,

and that is because we set

the default to 4.5 up here.

So I just wanted to show you

that here in this quick example.

Okay, so we were successful again,

so let's come back here, reload,

you're clicking this button, and indeed

we now get our two tours.

Great, and I remember how amazing it felt

for me when this worked the first time

when I was starting to learn Express and Mongo,

and how to build these applications,

and so I really hope that you're

feeling the same way right now,

as you just created your very first

documents using Express

and, of course, MongoDB.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.