Now in the last lesson we looked at how you might read and query your Mongo database for particular
documents in particular collections.
In this lesson I want to cover how you would update data in your database.
We're now further along in this CRUD documentation and we're now on the update operations.
You might remember previously from our SQL explorations we created the products table with a stock
value as well.
So the pen we have 32 of those and the pencil we had 12 of those.
So now we can learn about how to update our data inside the database by adding a stock value to each
of our documents.
At the moment, we have two products pen and pencil and they only have an id, a name and a price field.
If I wanted to update one of the records with a stock value as well then I might say something like
db.product.updateOne and inside my parentheses I will insert the query criteria.
So which document do I want to update.
Well I actually want to update the one that has an id of 1.
So that's going to be this document right here.
And this is my first input to this method
updateOne and the second input is going to be, well what do you want to update it to
right?
And so let me open up another set of curly braces and I'm going to use something called a set,
so $set
and this allows me to set a new field and value into my document.
Well I'm going to add a new field called stock and it's going to be 32.
And now I close off my method updateOne and I hit enter.
And you can see that my operation was acknowledged and the query matched one document and therefore
one document was modified or rather updated as specified here.
Now if I use that product.find again you can see that my pen now has a stock value associated with
it as well.
So now it's time for a challenge.
I want you to update our second document that has an id of 2 to have a stock field which has a value
of 12.
So we know that at the moment we have two records and we've managed to update our first record to have
a stock value of 32 but we now want this second record to also have a stock value and that's going to
be equal to 12.
So pause the video now and try to complete the challenge.
All right.
So we're going to use the same method that we saw in the documentation which is db.products.
updateOne.
And then we're going to specify the search or filter criteria for the record or the document that we
want to update.
And in this case it's going to be the one that has an id of 2.
And then the second parameter we put in here is going to be what we want to update about that record.
So we're going to use the set operator to set a new value for a new field and the field is going to
be called stock and the value is going to be 12 and then we close off our parentheses.
And now we have the updateOne method passing in two parameter
one is which record to update and the second is what to update about it.
So now if I hit enter, you can see that we've matched 1 record using our filter and we've modified one.
So that means if I now try and find all the products in my products collection, I now have pencil which
has a stock of 12 and pen with a stock of 32.