I am connected to a running mongod instance again and I'll use a dataset which we prepared in the insert
module, in case you skipped that, you find it attached,
you just need to import it into a new collection and database. It is this users dataset with a couple
of users in there.
Now here, I got some nice data and since this is the update module, now everything is about changing that
data
and let's start with Chris because Chris does not look like the other entries, the hobbies there are different,
there is no age,
there is no phone number,
so this is a perfect use case to update this data piece
and of course you would not just update from inside the shell,
it's of course typical that you build an application, let's say a blog where your users can edit a blog
post and once they hit save, you want to reflect that save in a database of course.
So how can we update Chris?
Well, an update operation requires two pieces of information, we need to be able to identify the document
that should be changed and then we need to define how it should change. For identification,
you can use any filter
you could also use for finding documents.
You don't have to use the ID,
you can find Chris by his name, by the hobbies, by a combination of name and hobbies,
this is all possible and you can also not just update one document at a time. If we want to update Chris,
I'll do it by ID which is a common scenario but as I just mentioned, not a must do, if I want to update
Chris,
I'll have a look at my persons
and there, I can use updateOne just like with insert one and insert many,
we have update one an update many and the names are pretty self-explanatory.
Update one simply takes the first document that matches your filter criteria and updates that even if
multiple documents would normally match your criteria,
update many will take your criteria, your filter and update all documents that match it.
So here I have update one,
now I add my filter criteria and I say ID should be equal to that objectid and then the second argument
in the update method is basically some information on how to update this object
and there you can use a variety of update operators.
Now you can find all update operators in the official docs in the reference under update operators
and I will walk you through the most important ones in this module of course.
Right now we're here in the field
update operators because I want to update a field or a couple of fields actually.
Now for that, we use $set
and we saw that earlier in the course already. $set simply takes a document where you describe
some fields that should be changed or added to the existing document,
so here I could say that I want to change hobbies,
hobbies already exists and it is an array and we have specific operators for working with arrays
but if I just want to completely override hobbies which is what I want to do here, I can use it here
together with set and then I simply define the new value for hobbies and this should still be an array,
hence the square brackets,
this will not add these elements to the existing array though, it will simply override the existing
hobbies.
So now I will look at a couple of documents in the array where each document has a title and then we
have sports and then a frequency like five, let's say, Chris is really sporty and then we add another document
and now these are really just the documents that will be inside the hobbies array.
So there we have another hobby with a title of cooking and a frequency of three let's say and then
we have a last document in that array where we have a title of hiking
and then we have a frequency of one. If I now hit enter, I get an error
because the collection is not called persons but users,
sorry about that
and I have a typo so I also should fix that,
it's of course update one, so little typo in there. The arguments there, that is all correct,
nothing needs to change there.
If I hit enter, we now get a feedback that it found one matching document,
so for our filter, this one, it found one matching document and it also modified one document because
the changes which we defined here made sense.
And with that I mean that if I would run this again, the exact same command, I actually still have a matched
count because we still have a matching document for the ID, the ID didn't change but it didn't modify
anything because the one change that I instructed here didn't make sense,
so mongodb ignored it because the value I wanted to set here is the already existing value.
Now it's also interesting to see that if I now find all users, that Chris still has his name and the ID,
so $set does not override the existing values
instead it simply just defines some changes that mongodb evaluates and then if they make sense,
it changes the existing document by adding or overriding these fields. All the existing fields are left
untouched,
they are not removed.
You can remove fields and I'll come to that in this module but by default, it simply just adds or edits
the fields which you specify,
so this is update one.
Now of course you're not limited to update one,
you could update many
and for that, I will find users who have and for that, let's quickly look into users who have a hobby with
title sports.
So let me quickly search for hobbies.title, that is how you can search for embedded documents in an
array and that should be equal to sports, close the
quotation marks, output this in a pretty way and you see we have Anna, Max and Chris.
Now let's say all persons who have that hobby should receive a new field which says isSporty
true,
so where I set a boolean value to true for a newly added field.
Now since I have three documents that should be updated, I could of course do it with update one three times
and manually search the ID here with the find method, copy the ID, execute update one or I take the simpler
route and simply replace find with update many and the filter stays the same and the second argument
then defines the change that should be applied to every document
and here again, I use $set to then set a new field which didn't exist before, isSporty to
true
and that is all.
If I now hit enter,
you see it found three documents and it modified three documents and if I now quickly search all users,
you'll see that Chris has isSporty true, Anna has isSporty true and Manuel does not have it because
there is no sports as a hobby and Max does have isSporty
true.
So this is now all set
with the help of update many and that's update one and update many and $set.