1
00:00:02,540 --> 00:00:05,130
Let's say we wanna update data,

2
00:00:05,130 --> 00:00:08,700
and for example, on my Munich Schnitzelhouse,

3
00:00:08,700 --> 00:00:12,280
I got a problem in the address, if you remember,

4
00:00:12,280 --> 00:00:15,610
or I included the street number

5
00:00:15,610 --> 00:00:18,830
and the wrong one too in the street

6
00:00:18,830 --> 00:00:22,140
where I do have a separate streetNumber field.

7
00:00:22,140 --> 00:00:25,130
So I might want to update this street field

8
00:00:25,130 --> 00:00:26,650
in the address field

9
00:00:26,650 --> 00:00:30,403
of my Munich Schnitzelhouse restaurant document.

10
00:00:31,570 --> 00:00:36,340
Now to do that, we can again use db and then the collection

11
00:00:36,340 --> 00:00:38,860
that holds the document we want to update,

12
00:00:38,860 --> 00:00:40,770
in my case restaurants,

13
00:00:40,770 --> 00:00:43,773
and then execute the updateOne

14
00:00:44,890 --> 00:00:48,610
or updateMany commands here,

15
00:00:48,610 --> 00:00:51,620
simply depending on whether you want to update one

16
00:00:51,620 --> 00:00:54,460
or multiple documents in one go.

17
00:00:54,460 --> 00:00:56,673
And here, I'll update one document,

18
00:00:56,673 --> 00:01:00,553
but the syntax will be the same for updating many documents.

19
00:01:01,810 --> 00:01:06,810
The first parameter value you should pass to updateOne

20
00:01:06,940 --> 00:01:09,760
is a JavaScript object so to say,

21
00:01:09,760 --> 00:01:12,750
so the curly braces opening and closing,

22
00:01:12,750 --> 00:01:15,930
which identifies the document,

23
00:01:15,930 --> 00:01:16,830
or in the case of updateMany,

24
00:01:16,830 --> 00:01:20,150
the documents that you want to update.

25
00:01:20,150 --> 00:01:22,690
Just as with find and findOne,

26
00:01:22,690 --> 00:01:25,970
here you filter for documents.

27
00:01:25,970 --> 00:01:30,850
And in my case, I actually do want to filter by id here.

28
00:01:30,850 --> 00:01:33,880
So before I execute this,

29
00:01:33,880 --> 00:01:35,550
let me delete this,

30
00:01:35,550 --> 00:01:39,480
I'll actually quickly find all documents,

31
00:01:39,480 --> 00:01:41,750
and then I'll grab this id here,

32
00:01:41,750 --> 00:01:43,633
so the part between the quotes.

33
00:01:44,600 --> 00:01:45,955
We can use this as well.

34
00:01:45,955 --> 00:01:50,955
Also for finding, you can just run db.restaurants.find

35
00:01:53,130 --> 00:01:55,730
and then search for an id.

36
00:01:55,730 --> 00:02:00,250
And here, you just wanna create such an ObjectId value

37
00:02:00,250 --> 00:02:03,440
which is a built-in value type that's created

38
00:02:03,440 --> 00:02:07,870
by executing ObjectId like a function like this,

39
00:02:07,870 --> 00:02:11,340
and then you pass your id between quotes to ObjectId

40
00:02:11,340 --> 00:02:16,340
and this will find you this entry here, this document.

41
00:02:16,498 --> 00:02:20,120
ObjectId is simply a built-in value type.

42
00:02:20,120 --> 00:02:23,410
And this is a creation function for such a value

43
00:02:23,410 --> 00:02:27,943
which is used internally by MongoDB for storing ids.

44
00:02:28,920 --> 00:02:30,510
So that's how we can use the id,

45
00:02:30,510 --> 00:02:33,200
and we can now use this for updating as well.

46
00:02:33,200 --> 00:02:38,200
For this, we run db.restaurants.updateOne in my case here,

47
00:02:38,340 --> 00:02:42,420
and then search for the document with this id

48
00:02:42,420 --> 00:02:45,490
as I just showed it to you, like that.

49
00:02:45,490 --> 00:02:48,120
And then the second parameter value

50
00:02:48,120 --> 00:02:51,380
now defines how you want to update this one,

51
00:02:51,380 --> 00:02:52,940
or with updateMany,

52
00:02:52,940 --> 00:02:55,963
the many documents that might have been identified.

53
00:02:57,090 --> 00:03:01,220
For this, you pass in another JavaScript object, so to say.

54
00:03:01,220 --> 00:03:04,840
Whoops, and now I accidentally pressed enter here.

55
00:03:04,840 --> 00:03:07,870
So now in here, between those curly braces,

56
00:03:07,870 --> 00:03:11,060
you now define how that document should be updated.

57
00:03:11,060 --> 00:03:15,870
And for this, you use a special key that's reserved,

58
00:03:15,870 --> 00:03:17,460
that's understood by MongoDB,

59
00:03:17,460 --> 00:03:19,943
and that's the $set key.

60
00:03:21,210 --> 00:03:24,230
This is not a key name that's up to you.

61
00:03:24,230 --> 00:03:27,500
Instead, that's a special key MongoDB will be looking for,

62
00:03:27,500 --> 00:03:30,210
and that takes another object,

63
00:03:30,210 --> 00:03:33,990
so another pair of curly braces, as a value.

64
00:03:33,990 --> 00:03:36,510
And it's now between those curly braces

65
00:03:36,510 --> 00:03:39,813
where you define the actual updating you want to do.

66
00:03:40,800 --> 00:03:45,590
So here, you can now access the properties to keys

67
00:03:45,590 --> 00:03:47,813
of your document.

68
00:03:49,210 --> 00:03:53,670
For example, if I would want to update the restaurant name,

69
00:03:53,670 --> 00:03:55,350
here we could type name,

70
00:03:55,350 --> 00:03:58,170
since that's the key name that stores the restaurant name,

71
00:03:58,170 --> 00:04:01,407
and then our new value, like, "Some new name."

72
00:04:02,670 --> 00:04:04,740
That's adding a line break again for me here,

73
00:04:04,740 --> 00:04:06,703
but it's still one and the same command.

74
00:04:06,703 --> 00:04:08,600
It just added the line break

75
00:04:08,600 --> 00:04:10,500
because I'm running out of space here.

76
00:04:12,040 --> 00:04:14,850
Now I don't wanna update the name here though,

77
00:04:14,850 --> 00:04:18,110
but the great thing about $set and the update command

78
00:04:18,110 --> 00:04:22,710
is that you can also update nested document values.

79
00:04:22,710 --> 00:04:26,400
Then you just need to describe the path to that value

80
00:04:26,400 --> 00:04:28,330
by using quotes here,

81
00:04:28,330 --> 00:04:31,630
and then here we just want to dive into the address key,

82
00:04:31,630 --> 00:04:34,060
which holds that nested object,

83
00:04:34,060 --> 00:04:38,193
and then there with dot into the street key.

84
00:04:39,620 --> 00:04:41,860
And with that, we're telling MongoDB

85
00:04:41,860 --> 00:04:46,120
that we wanna assign a new value to the street key

86
00:04:46,120 --> 00:04:48,393
in the nested address object.

87
00:04:49,740 --> 00:04:53,120
And then here, we could rename this to Some Street

88
00:04:53,120 --> 00:04:56,280
and change it from Some Street 5, which it was before,

89
00:04:56,280 --> 00:04:57,763
to just Some Street.

90
00:04:59,320 --> 00:05:00,760
And if you now hit enter,

91
00:05:00,760 --> 00:05:03,100
you get this confirmation dialog

92
00:05:03,100 --> 00:05:06,380
where you also see how many documents it found

93
00:05:06,380 --> 00:05:09,310
and how many documents it updated.

94
00:05:09,310 --> 00:05:13,923
And if you now again run db.restaurants find here,

95
00:05:13,923 --> 00:05:16,760
you see that here indeed I messed up

96
00:05:16,760 --> 00:05:18,430
and updated the wrong document,

97
00:05:18,430 --> 00:05:21,740
but still, the update succeeded.

98
00:05:21,740 --> 00:05:24,410
Still, it was a small fail from my side.

99
00:05:24,410 --> 00:05:26,850
I did want to update this document,

100
00:05:26,850 --> 00:05:29,960
so I'll quickly fix this by copying this id

101
00:05:29,960 --> 00:05:32,816
and then replacing the id in the update command

102
00:05:32,816 --> 00:05:35,550
with that new id here.

103
00:05:35,550 --> 00:05:38,470
So I'll erase that and use that.

104
00:05:38,470 --> 00:05:41,083
And now with that, this was updated.

105
00:05:42,580 --> 00:05:47,580
And if I now run db.restaurants.find here,

106
00:05:47,880 --> 00:05:50,890
now that was changed to Some Street.

107
00:05:50,890 --> 00:05:53,300
Now they have the same street because I messed up,

108
00:05:53,300 --> 00:05:55,870
but at least different street numbers.

109
00:05:55,870 --> 00:05:59,623
And that's how you can update one or multiple documents.

