All language subtitles for 004 Updating and Deleting Data Using Mongoose_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 Download
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
fa Persian
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

So now that we've covered how you might create new documents and new collections into our database using

Mongoose and we also in the last lesson looked at how you might validate new data that's being entered

into the database,

now I want to cover the last parts of CRUD and briefly show you how you might update and delete data

using Mongoose.

If you remember previously we added in that Peach but we forgot to add an a name for it.

This was allowed to happen because we didn't have the validation in place yet, we didn't set the name

field as required

when this happened.

Let's use this opportunity to learn about how you would update data and let's give that document a name

of Peach. I'm going to comment out my fruit.safe and down here just below the find

we're going to update our peach.

So we're going to again tap into that fruits collection through the fruit model and we're going to call

the method updateOne.

The reason I know how to do this is again because of the documentation. Inside the API documentation

for all of our models

you can see there's findOne,

there's also update, update

Many and updateOne. You can use all three of these depending on what you need

but the one that I've chosen is updateOne and it has three required parameters and one optional parameter.

What we have to specify is the condition to filter upon, so which document do you want to update basically.

And then the next one is what are you going to update.

And finally it accepts a callback which allows us to log any errors if there are any. Inside our code

here

we've called fruit.updateOne and now we're going to specify our first parameter which is going to

be the filter or what it is that we want to update.

In our case, I want to update the record that has a particular ID

and I can see that ID in my Mongo shell and it's this string over here which was generated automatically

when we created this document. And that ID corresponds to the document that has the peach without a name.

So if I copy that over and I paste it in here then I now have my query.

So this is the item that I want to update.

It has an ID that is specific to it

and it's this. The next thing that I can add is what it is that I want to update about it.

In this case I'm going to update the name field and I'm going to set it to Peach.

And finally the third parameter is simply going to be a callback function which will log any errors

if there are any.

So if error, then we'll log the error

but if there weren't any errors, then we'll just log "Successfully updated the document".

Now let's close everything off and hit save and let's go into our hyper terminal and run on node app.

js

again with that update and you can see now it says "Successfully updated the document"

and if we go over to our Mongo shell and we look at all the fruits inside our fruit database, you can

see that we don't have any extra documents.

We've still only got five.

But this last one now has the name field filled and it now has the value of Peach.

So now if we run it again and it logs all of our fruits, you can see we now have apple, kiwi, orange, banana

and peach.

All of our data is now updated and complete. Over here

you might see that the field Peach got added in right at the end to this document but the order doesn't

actually matter because we can tap into every single property using the dot notation.

So it'll search through this entire document and find the relevant piece of data for you just by writing

that fruit

.name. The final thing that we want to do is what if we wanted to delete a particular record.

Let's say I'm going to delete this peach after all.

Well then we could use the deleteOne method.

I'm going to leave this as a challenge for you. Based off this documentation

I want you to delete the peach entry in our fruits collection. If you succeed

when I run in my Mongo shell the same fruits.find I should no longer see this last record.

Pause the video now and see if you can complete this challenge.

They've actually very helpfully included an example for you to show you how you might call this delete

One function. And it only has two parameters:

one is the conditions.

This is again what exactly do you want to delete.

You need it to match with something right> And then the second one is a callback.

Again this allows us to log any errors that occurred when we were deleting the document.

Let's implement that in our code.

So I'm going to comment out our updateOne

and just below it I'm going to say fruit.deleteOne. And the first parameter is of course the condition,

what do I want to delete.

Well I want to delete the document that has a name of Peach. You can also use the ID or anything else

that's unique about it,

but in this case I know that I have a document that's called Peach.

Next I'm going to specify the callback function which only has an error as a parameter.

And again I'm going to check if there was an error,

I will flag the error but if there were no errors then I'm going to log successfully.

"Successfully deleted the document".

Now let's go ahead and run this code in our hyper terminal and see what happens.

So now we've got successfully "delted" the

document, a typo there from me.

But when it loops through our data to find all of the documents, we no longer have a peach being shown.

And again if I switch over to my Mongo shell and run db.fruits.find we now only have four

entries with that last one where the name matches Peach being deleted.

The other function that we might want to use is to deleteMany because sometimes you might want to just

clear out a collection or delete a number of documents that match a particular condition.

If you remember previously when we were running our app.js, for a couple of times we didn't actually

comment out the person.save.

And that means that every time we ran app.js, we created that same person John who's 37, got saved

into our database many many times. If you head over to your Mongo shell and you wrote "show collections"

you can see we have our fruits and our people.

And if you say "db.people.find" to show us all the data in there, we have loads of Johns who are

37. As a challenge

I want you to delete all of the data in here where the name matches with John and I want you to clear

out this collection

through the use of this deleteMany method. Pause the video and try to complete the challenge.

This is the syntax that we need to follow.

We're going to tap into a particular model and we're going to call the deleteMany method. And then we're

going to pass in the required two parameters which is the condition which matches the document we want

to delete and the callback to log any errors. Down here let's comment out our deleteOne and instead let's

now tap into the people's collection by typing person which is the name of the model that is linked

to the people's collection. Person

.deleteMany

and then the first parameter is of course the condition.

So we said that the name has to match John.

And then the second parameter was just a callback with an arrow

and if there was an error log the error

else log "Successfully deleted

all the documents".

Now let's run this code and clear out our collection.

So" successfully deleted all the documents" documents.

And now if we do db.people.find again, you can see it's completely empty.

We've deleted all of these records where the name matches with John. To ensure that we only have one

John

let's go ahead and save a single one of these.

And now we should have a single John in our records

and now let's comment this out again before we start adding more Johns into our people's collection.

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