‫Welcome back.
‫I hope you're having fun so far in this section.
‫I know that I am and so let's now keep it moving
‫and learning how to update documents with Mongodb.
‫So back in our mongoshell let's now
‫use db.tours.updateOne.
‫Okay?
‫So that's similar to .insertone that we used
‫to create a new document.
‫And remember how we also had .insertmany
‫and so we also have .updatemany.
‫Now how does .updatemany work?
‫Well, first we need to select which documents
‫we actually want to update and, second, we need
‫to pass in the data that should be updated.
‫So the first argument is basically a filter object
‫just like we learned in the last lecture.
‫So we basically need to query for the documents
‫that we want to update and let's start with that
‫starting in a very simple way.
‫So I'm just going to select based
‫on the name
‫The Snow Adventure.
‫Okay?
‫So that is my very simple filter object
‫so specifying the tour that I want to update
‫And now the second object that I need
‫to specify is what I actually want to update.
‫So another object here and then we close the function.
‫And so in here we need to use the set operator.
‫So, again, this works using operators just like
‫in these complex queries that we saw in the last video.
‫So set and then we need to create yet another object
‫and finally in this one we can specify the property
‫that we want to update and the value we want to set it to.
‫So, in this case, what we want to do
‫is to set the price to 597.
‫All right?
‫So this one was 497 and so we're now, basically,
‫increasing it to 597.
‫All right?
‫So let's hit return and it's then updated, our tour.
‫Now if this query here would have matched
‫multiple documents.
‫I can't select it for some reason,
‫but you can see it.
‫So if this query that we did here
‫would have selected multiple documents then only
‫the first one would have been updated because
‫we were using .updatemain.
‫And so if we already know beforehand that our query
‫is going to match multiple documents,
‫then we should use .updatemany
‫and not .updateone.
‫Okay?
‫Now just to show you that it actually worked,
‫let's take a look at all the documents.
‫So db.tours.find and, indeed,
‫we have The Snow Adventure here
‫with the new price.
‫Great.
‫So we updated a property that already existed.
‫So the price was already there and we simply set it
‫to a new value but we can also create new properties
‫and set these to new values, of course.
‫And it actually works in the same way but
‫let me show it to you anyway.
‫And what I want to do here is to find premium tours
‫and give them a premium field set to true.
‫So what are our premium tours?
‫Well, let's first do a find.
‫So just to specify that query.
‫And actually we already did that before.
‫So the tours that I want should have a price greater
‫than 500 and a rating greater or equal to 4.8.
‫So these are the really our premium tours, basically,
‫so really the ones that are most expensive
‫and most well rated.
‫So to create this query its actually quite simple.
‫And again because we actually already did it before.
‫Now what I want you to do here is to pause the video
‫and even if you remember it,
‫type out this query on your own.
‫And if you don't remember it, well that's even better
‫then you can practice what you just learned
‫in the last video.
‫All right?
‫So please pause the video here and try to come up
‫with this query on your own.
‫So again we want the price to be greater than
‫500 and the rating greater or equal to 4.8.
‫Okay, hope you did it.
‫And this is the solution.
‫So price should be greater or equal than 500
‫and the rating should be greater or equal to 4.8.
‫Okay?
‫And here we set just greater than which
‫is actually the same.
‫It's gonna give us the same result.
‫But never mind, let's do it exactly as I said.
‫And so just to make sure let's see if we get the
‫perfect result and indeed this is the tour
‫that has this premium price and at the same time
‫this premium rating.
‫Okay?
‫And in this case, we have only one result but,
‫of course, we might have multiple results.
‫And so let's now use .updatemany.
‫So db.tours.updatemany.
‫And so our filter object that we want is this one.
‫So I'm just copying it from here.
‫So that is the first argument and then second
‫is actually what we want to update.
‫Remember that?
‫So again we use the set operator and then
‫we say that we want premium to be true.
‫Okay?
‫And so this time we actually create a new field here
‫and we set it to true.
‫Okay?
‫Give that one a save and actually we were missing
‫some closing brace and so we cannot really go back
‫so let me try to add it here.
‫But, that doesn't really work, but anyway it doesn't
‫matter we can just go back to the last command
‫and then correct it.
‫So this is the brace that we were missing then
‫close it and here we go.
‫So db.tours.find just to check it now.
‫And here we have the last one which now
‫has premium set to true.
‫And again this was the only document that matched
‫our query and so it's the only one that got this new
‫premium property here,
‫but if there were multiple documents matching
‫that query, then all of them would have gotten
‫this premium true.
‫Okay?
‫So that's why we used .updatemany and remember
‫if we used .updateone then only the first document
‫matching the query gets updated.
‫Okay?
‫So that's how we update documents at least partially
‫so with this .updatemany or .updateone we usually only
‫update parts of the document, but we can also
‫completely replace the content of the document.
‫And for that we use .replaceone.
‫I'm not going to do that, but for the sake
‫of completeness I wanted to show it to you as well.
‫So db.tours.replaceone or .replacemany, okay?
‫And so in here, just like before, you would
‫pass the search query and then new data that you
‫want to put in this document.
‫All right, not going to do that here.
‫But now you know that it also exists.
‫And like this we have three of our correct operations
‫already completed.
‫So creating, reading, and updating and so
‫as you can guess in the next video we will talk
‫about deleting documents.