WEBVTT 0 00:00.480 --> 00:07.350 Now in the last lesson we looked at how you might read and query your Mongo database for particular 1 00:07.350 --> 00:09.900 documents in particular collections. 2 00:09.900 --> 00:15.240 In this lesson I want to cover how you would update data in your database. 3 00:15.270 --> 00:21.290 We're now further along in this CRUD documentation and we're now on the update operations. 4 00:21.480 --> 00:29.580 You might remember previously from our SQL explorations we created the products table with a stock 5 00:29.640 --> 00:30.670 value as well. 6 00:30.690 --> 00:35.280 So the pen we have 32 of those and the pencil we had 12 of those. 7 00:35.310 --> 00:42.210 So now we can learn about how to update our data inside the database by adding a stock value to each 8 00:42.270 --> 00:44.070 of our documents. 9 00:44.070 --> 00:52.340 At the moment, we have two products pen and pencil and they only have an id, a name and a price field. 10 00:52.410 --> 01:00.420 If I wanted to update one of the records with a stock value as well then I might say something like 11 01:00.420 --> 01:11.160 db.product.updateOne and inside my parentheses I will insert the query criteria. 12 01:11.160 --> 01:14.060 So which document do I want to update. 13 01:14.250 --> 01:19.380 Well I actually want to update the one that has an id of 1. 14 01:19.380 --> 01:22.710 So that's going to be this document right here. 15 01:23.160 --> 01:26.240 And this is my first input to this method 16 01:26.250 --> 01:31.920 updateOne and the second input is going to be, well what do you want to update it to 17 01:31.950 --> 01:32.500 right? 18 01:32.730 --> 01:39.100 And so let me open up another set of curly braces and I'm going to use something called a set, 19 01:39.120 --> 01:41.820 so $set 20 01:42.090 --> 01:47.840 and this allows me to set a new field and value into my document. 21 01:48.150 --> 01:52.870 Well I'm going to add a new field called stock and it's going to be 32. 22 01:53.310 --> 01:59.140 And now I close off my method updateOne and I hit enter. 23 01:59.430 --> 02:05.280 And you can see that my operation was acknowledged and the query matched one document and therefore 24 02:05.280 --> 02:10.970 one document was modified or rather updated as specified here. 25 02:11.250 --> 02:18.960 Now if I use that product.find again you can see that my pen now has a stock value associated with 26 02:18.960 --> 02:20.640 it as well. 27 02:20.640 --> 02:23.500 So now it's time for a challenge. 28 02:23.550 --> 02:31.290 I want you to update our second document that has an id of 2 to have a stock field which has a value 29 02:31.290 --> 02:32.390 of 12. 30 02:32.820 --> 02:40.540 So we know that at the moment we have two records and we've managed to update our first record to have 31 02:40.570 --> 02:47.590 a stock value of 32 but we now want this second record to also have a stock value and that's going to 32 02:47.590 --> 02:48.920 be equal to 12. 33 02:49.060 --> 02:52.500 So pause the video now and try to complete the challenge. 34 02:54.570 --> 02:54.890 All right. 35 02:54.900 --> 03:01.860 So we're going to use the same method that we saw in the documentation which is db.products. 36 03:02.190 --> 03:03.610 updateOne. 37 03:04.080 --> 03:10.050 And then we're going to specify the search or filter criteria for the record or the document that we 38 03:10.050 --> 03:11.140 want to update. 39 03:11.370 --> 03:15.480 And in this case it's going to be the one that has an id of 2. 40 03:16.350 --> 03:22.050 And then the second parameter we put in here is going to be what we want to update about that record. 41 03:22.080 --> 03:29.250 So we're going to use the set operator to set a new value for a new field and the field is going to 42 03:29.250 --> 03:35.430 be called stock and the value is going to be 12 and then we close off our parentheses. 43 03:35.550 --> 03:40.410 And now we have the updateOne method passing in two parameter 44 03:40.410 --> 03:47.070 one is which record to update and the second is what to update about it. 45 03:47.070 --> 03:54.910 So now if I hit enter, you can see that we've matched 1 record using our filter and we've modified one. 46 03:55.020 --> 04:02.250 So that means if I now try and find all the products in my products collection, I now have pencil which 47 04:02.250 --> 04:06.100 has a stock of 12 and pen with a stock of 32.