So now that we've covered how you might create new documents and new collections into our database using 1
Mongoose and we also in the last lesson looked at how you might validate new data that's being entered 2
into the database, 3
now I want to cover the last parts of CRUD and briefly show you how you might update and delete data 4
using Mongoose. 5
If you remember previously we added in that Peach but we forgot to add an a name for it. 6
This was allowed to happen because we didn't have the validation in place yet, we didn't set the name 7
field as required 8
when this happened. 9
Let's use this opportunity to learn about how you would update data and let's give that document a name 10
of Peach. I'm going to comment out my fruit.safe and down here just below the find 11
we're going to update our peach. 12
So we're going to again tap into that fruits collection through the fruit model and we're going to call 13
the method updateOne. 14
The reason I know how to do this is again because of the documentation. Inside the API documentation 15
for all of our models 16
you can see there's findOne, 17
there's also update, update 18
Many and updateOne. You can use all three of these depending on what you need 19
but the one that I've chosen is updateOne and it has three required parameters and one optional parameter. 20
What we have to specify is the condition to filter upon, so which document do you want to update basically. 21
And then the next one is what are you going to update. 22
And finally it accepts a callback which allows us to log any errors if there are any. Inside our code 23
here 24
we've called fruit.updateOne and now we're going to specify our first parameter which is going to 25
be the filter or what it is that we want to update. 26
In our case, I want to update the record that has a particular ID 27
and I can see that ID in my Mongo shell and it's this string over here which was generated automatically 28
when we created this document. And that ID corresponds to the document that has the peach without a name. 29
So if I copy that over and I paste it in here then I now have my query. 30
So this is the item that I want to update. 31
It has an ID that is specific to it 32
and it's this. The next thing that I can add is what it is that I want to update about it. 33
In this case I'm going to update the name field and I'm going to set it to Peach. 34
And finally the third parameter is simply going to be a callback function which will log any errors 35
if there are any. 36
So if error, then we'll log the error 37
but if there weren't any errors, then we'll just log "Successfully updated the document". 38
Now let's close everything off and hit save and let's go into our hyper terminal and run on node app. 39
js 40
again with that update and you can see now it says "Successfully updated the document" 41
and if we go over to our Mongo shell and we look at all the fruits inside our fruit database, you can 42
see that we don't have any extra documents. 43
We've still only got five. 44
But this last one now has the name field filled and it now has the value of Peach. 45
So now if we run it again and it logs all of our fruits, you can see we now have apple, kiwi, orange, banana 46
and peach. 47
All of our data is now updated and complete. Over here 48
you might see that the field Peach got added in right at the end to this document but the order doesn't 49
actually matter because we can tap into every single property using the dot notation. 50
So it'll search through this entire document and find the relevant piece of data for you just by writing 51
that fruit 52
.name. The final thing that we want to do is what if we wanted to delete a particular record. 53
Let's say I'm going to delete this peach after all. 54
Well then we could use the deleteOne method. 55
I'm going to leave this as a challenge for you. Based off this documentation 56
I want you to delete the peach entry in our fruits collection. If you succeed 57
when I run in my Mongo shell the same fruits.find I should no longer see this last record. 58
Pause the video now and see if you can complete this challenge. 59
They've actually very helpfully included an example for you to show you how you might call this delete 60
One function. And it only has two parameters: 61
one is the conditions. 62
This is again what exactly do you want to delete. 63
You need it to match with something right> And then the second one is a callback. 64
Again this allows us to log any errors that occurred when we were deleting the document. 65
Let's implement that in our code. 66
So I'm going to comment out our updateOne 67
and just below it I'm going to say fruit.deleteOne. And the first parameter is of course the condition, 68
what do I want to delete. 69
Well I want to delete the document that has a name of Peach. You can also use the ID or anything else 70
that's unique about it, 71
but in this case I know that I have a document that's called Peach. 72
Next I'm going to specify the callback function which only has an error as a parameter. 73
And again I'm going to check if there was an error, 74
I will flag the error but if there were no errors then I'm going to log successfully. 75
"Successfully deleted the document". 76
Now let's go ahead and run this code in our hyper terminal and see what happens. 77
So now we've got successfully "delted" the 78
document, a typo there from me. 79
But when it loops through our data to find all of the documents, we no longer have a peach being shown. 80
And again if I switch over to my Mongo shell and run db.fruits.find we now only have four 81
entries with that last one where the name matches Peach being deleted. 82
The other function that we might want to use is to deleteMany because sometimes you might want to just 83
clear out a collection or delete a number of documents that match a particular condition. 84
If you remember previously when we were running our app.js, for a couple of times we didn't actually 85
comment out the person.save. 86
And that means that every time we ran app.js, we created that same person John who's 37, got saved 87
into our database many many times. If you head over to your Mongo shell and you wrote "show collections" 88
you can see we have our fruits and our people. 89
And if you say "db.people.find" to show us all the data in there, we have loads of Johns who are 90
37. As a challenge 91
I want you to delete all of the data in here where the name matches with John and I want you to clear 92
out this collection 93
through the use of this deleteMany method. Pause the video and try to complete the challenge. 94
This is the syntax that we need to follow. 95
We're going to tap into a particular model and we're going to call the deleteMany method. And then we're 96
going to pass in the required two parameters which is the condition which matches the document we want 97
to delete and the callback to log any errors. Down here let's comment out our deleteOne and instead let's 98
now tap into the people's collection by typing person which is the name of the model that is linked 99
to the people's collection. Person 100
.deleteMany 101
and then the first parameter is of course the condition. 102
So we said that the name has to match John. 103
And then the second parameter was just a callback with an arrow 104
and if there was an error log the error 105
else log "Successfully deleted 106
all the documents". 107
Now let's run this code and clear out our collection. 108
So" successfully deleted all the documents" documents. 109
And now if we do db.people.find again, you can see it's completely empty. 110
We've deleted all of these records where the name matches with John. To ensure that we only have one 111
John 112
let's go ahead and save a single one of these. 113
And now we should have a single John in our records 114
and now let's comment this out again before we start adding more Johns into our people's collection.