Let's say we wanna update data,
and for example, on my Munich Schnitzelhouse,
I got a problem in the address, if you remember,
or I included the street number
and the wrong one too in the street
where I do have a separate streetNumber field.
So I might want to update this street field
in the address field
of my Munich Schnitzelhouse restaurant document.
Now to do that, we can again use db and then the collection
that holds the document we want to update,
in my case restaurants,
and then execute the updateOne
or updateMany commands here,
simply depending on whether you want to update one
or multiple documents in one go.
And here, I'll update one document,
but the syntax will be the same for updating many documents.
The first parameter value you should pass to updateOne
is a JavaScript object so to say,
so the curly braces opening and closing,
which identifies the document,
or in the case of updateMany,
the documents that you want to update.
Just as with find and findOne,
here you filter for documents.
And in my case, I actually do want to filter by id here.
So before I execute this,
let me delete this,
I'll actually quickly find all documents,
and then I'll grab this id here,
so the part between the quotes.
We can use this as well.
Also for finding, you can just run db.restaurants.find
and then search for an id.
And here, you just wanna create such an ObjectId value
which is a built-in value type that's created
by executing ObjectId like a function like this,
and then you pass your id between quotes to ObjectId
and this will find you this entry here, this document.
ObjectId is simply a built-in value type.
And this is a creation function for such a value
which is used internally by MongoDB for storing ids.
So that's how we can use the id,
and we can now use this for updating as well.
For this, we run db.restaurants.updateOne in my case here,
and then search for the document with this id
as I just showed it to you, like that.
And then the second parameter value
now defines how you want to update this one,
or with updateMany,
the many documents that might have been identified.
For this, you pass in another JavaScript object, so to say.
Whoops, and now I accidentally pressed enter here.
So now in here, between those curly braces,
you now define how that document should be updated.
And for this, you use a special key that's reserved,
that's understood by MongoDB,
and that's the $set key.
This is not a key name that's up to you.
Instead, that's a special key MongoDB will be looking for,
and that takes another object,
so another pair of curly braces, as a value.
And it's now between those curly braces
where you define the actual updating you want to do.
So here, you can now access the properties to keys
of your document.
For example, if I would want to update the restaurant name,
here we could type name,
since that's the key name that stores the restaurant name,
and then our new value, like, "Some new name."
That's adding a line break again for me here,
but it's still one and the same command.
It just added the line break
because I'm running out of space here.
Now I don't wanna update the name here though,
but the great thing about $set and the update command
is that you can also update nested document values.
Then you just need to describe the path to that value
by using quotes here,
and then here we just want to dive into the address key,
which holds that nested object,
and then there with dot into the street key.
And with that, we're telling MongoDB
that we wanna assign a new value to the street key
in the nested address object.
And then here, we could rename this to Some Street
and change it from Some Street 5, which it was before,
to just Some Street.
And if you now hit enter,
you get this confirmation dialog
where you also see how many documents it found
and how many documents it updated.
And if you now again run db.restaurants find here,
you see that here indeed I messed up
and updated the wrong document,
but still, the update succeeded.
Still, it was a small fail from my side.
I did want to update this document,
so I'll quickly fix this by copying this id
and then replacing the id in the update command
with that new id here.
So I'll erase that and use that.
And now with that, this was updated.
And if I now run db.restaurants.find here,
now that was changed to Some Street.
Now they have the same street because I messed up,
but at least different street numbers.
And that's how you can update one or multiple documents.