Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,480 --> 00:00:07,350
Now in the last lesson we looked at how you might read and query your Mongo database for particular
2
00:00:07,350 --> 00:00:09,900
documents in particular collections.
3
00:00:09,900 --> 00:00:15,240
In this lesson I want to cover how you would update data in your database.
4
00:00:15,270 --> 00:00:21,290
We're now further along in this CRUD documentation and we're now on the update operations.
5
00:00:21,480 --> 00:00:29,580
You might remember previously from our SQL explorations we created the products table with a stock
6
00:00:29,640 --> 00:00:30,670
value as well.
7
00:00:30,690 --> 00:00:35,280
So the pen we have 32 of those and the pencil we had 12 of those.
8
00:00:35,310 --> 00:00:42,210
So now we can learn about how to update our data inside the database by adding a stock value to each
9
00:00:42,270 --> 00:00:44,070
of our documents.
10
00:00:44,070 --> 00: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.
11
00:00:52,410 --> 00:01:00,420
If I wanted to update one of the records with a stock value as well then I might say something like
12
00:01:00,420 --> 00:01:11,160
db.product.updateOne and inside my parentheses I will insert the query criteria.
13
00:01:11,160 --> 00:01:14,060
So which document do I want to update.
14
00:01:14,250 --> 00:01:19,380
Well I actually want to update the one that has an id of 1.
15
00:01:19,380 --> 00:01:22,710
So that's going to be this document right here.
16
00:01:23,160 --> 00:01:26,240
And this is my first input to this method
17
00:01:26,250 --> 00:01:31,920
updateOne and the second input is going to be, well what do you want to update it to
18
00:01:31,950 --> 00:01:32,500
right?
19
00:01:32,730 --> 00:01:39,100
And so let me open up another set of curly braces and I'm going to use something called a set,
20
00:01:39,120 --> 00:01:41,820
so $set
21
00:01:42,090 --> 00:01:47,840
and this allows me to set a new field and value into my document.
22
00:01:48,150 --> 00:01:52,870
Well I'm going to add a new field called stock and it's going to be 32.
23
00:01:53,310 --> 00:01:59,140
And now I close off my method updateOne and I hit enter.
24
00:01:59,430 --> 00:02:05,280
And you can see that my operation was acknowledged and the query matched one document and therefore
25
00:02:05,280 --> 00:02:10,970
one document was modified or rather updated as specified here.
26
00:02:11,250 --> 00:02:18,960
Now if I use that product.find again you can see that my pen now has a stock value associated with
27
00:02:18,960 --> 00:02:20,640
it as well.
28
00:02:20,640 --> 00:02:23,500
So now it's time for a challenge.
29
00:02:23,550 --> 00: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
30
00:02:31,290 --> 00:02:32,390
of 12.
31
00:02:32,820 --> 00: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
32
00:02:40,570 --> 00: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
33
00:02:47,590 --> 00:02:48,920
be equal to 12.
34
00:02:49,060 --> 00:02:52,500
So pause the video now and try to complete the challenge.
35
00:02:54,570 --> 00:02:54,890
All right.
36
00:02:54,900 --> 00:03:01,860
So we're going to use the same method that we saw in the documentation which is db.products.
37
00:03:02,190 --> 00:03:03,610
updateOne.
38
00:03:04,080 --> 00:03:10,050
And then we're going to specify the search or filter criteria for the record or the document that we
39
00:03:10,050 --> 00:03:11,140
want to update.
40
00:03:11,370 --> 00:03:15,480
And in this case it's going to be the one that has an id of 2.
41
00:03:16,350 --> 00:03:22,050
And then the second parameter we put in here is going to be what we want to update about that record.
42
00:03:22,080 --> 00: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
43
00:03:29,250 --> 00:03:35,430
be called stock and the value is going to be 12 and then we close off our parentheses.
44
00:03:35,550 --> 00:03:40,410
And now we have the updateOne method passing in two parameter
45
00:03:40,410 --> 00:03:47,070
one is which record to update and the second is what to update about it.
46
00:03:47,070 --> 00: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.
47
00:03:55,020 --> 00:04:02,250
So that means if I now try and find all the products in my products collection, I now have pencil which
48
00:04:02,250 --> 00:04:06,100
has a stock of 12 and pen with a stock of 32.
4948
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.