1 00:00:02,180 --> 00:00:10,400 Now that we worked quite a bit on the different field operators, before we talk about array operators which 2 00:00:10,400 --> 00:00:15,730 are nice operators that help us with working and updating array values, 3 00:00:15,770 --> 00:00:22,130 let's dive into a nice option you can pass to update one and update many. 4 00:00:22,170 --> 00:00:26,990 First of all, let's quickly have a look at our persons again. 5 00:00:27,060 --> 00:00:32,520 These are the four documents we have, Max, Manuel, Anna and Chris 6 00:00:32,520 --> 00:00:40,500 and now let's say we want to update some document where we are not sure whether it exists in the collection 7 00:00:40,500 --> 00:00:41,300 or not 8 00:00:41,380 --> 00:00:48,060 beause you have an application where you don't know if the data was saved to the database yet and if 9 00:00:48,060 --> 00:00:51,830 it wasn't saved yet, you now want to create a new document, 10 00:00:52,020 --> 00:00:58,590 if it was, you want to override the existing or update the existing document and that is something you can 11 00:00:58,590 --> 00:01:07,950 do with db.users update one or many, works for both, 12 00:01:07,970 --> 00:01:12,700 let's say we're looking for name Maria and Maria does not exist here, 13 00:01:12,710 --> 00:01:13,220 important, 14 00:01:13,220 --> 00:01:14,020 we got Chris, 15 00:01:14,020 --> 00:01:17,580 we got Anna, we got Manuel, we got Max, we got no Maria here. 16 00:01:17,900 --> 00:01:23,960 So we're looking for the name Maria and I want to update this value. 17 00:01:24,330 --> 00:01:26,740 Now I will do that with $set 18 00:01:27,030 --> 00:01:30,930 and here let's say I'll set the age to 29, 19 00:01:30,930 --> 00:01:42,450 I'll set hobbies to one hobby to not type too much here, let's say title is good food, whoops, and the frequency 20 00:01:42,510 --> 00:01:45,550 is three times per week whatever 21 00:01:45,900 --> 00:01:49,040 and I also set isSporty to 22 00:01:49,140 --> 00:01:51,900 true, like that. 23 00:01:51,900 --> 00:01:57,720 Now if I hit enter, we found no magic document and hence no change was made. 24 00:01:57,720 --> 00:02:03,780 Now as I mentioned, maybe in such a scenario, we don't want to get this information and then manually insert 25 00:02:03,840 --> 00:02:07,740 that person but we want to let mongodb do that for us, 26 00:02:07,770 --> 00:02:15,140 either update or insert. And you can do that by passing a third argument to update one, 27 00:02:15,150 --> 00:02:20,370 so this is now not something you pass to your updating description but as a third argument to the 28 00:02:20,390 --> 00:02:27,420 update one method and there, there is a nice option you can set which is called upsert and you can set this 29 00:02:27,420 --> 00:02:28,020 to true, 30 00:02:28,050 --> 00:02:33,790 the default is false and you don't need to set the default of course. Now upsert 31 00:02:34,000 --> 00:02:40,440 simply is a combination of the word update and insert and means that if the document does not exist, 32 00:02:40,860 --> 00:02:42,390 it will be created. 33 00:02:42,840 --> 00:02:49,890 So now if I set this to true and I hit enter, you see it actually did not find anything, did not update 34 00:02:49,890 --> 00:02:54,710 anything but it upserted a document with this new ID. 35 00:02:55,350 --> 00:03:01,120 And if I now look into my objects, you see that this document was added 36 00:03:01,290 --> 00:03:09,150 and please note that it even set the name to Maria even though I did not add this in my update one 37 00:03:09,150 --> 00:03:12,230 operation, I filtered for this name 38 00:03:12,390 --> 00:03:20,970 but when I set values, I only set the age, the hobbies and isSporty. Now it automatically set a name 39 00:03:20,970 --> 00:03:27,630 because correctly mongodb assumed that for whatever you are filtering here, that this is something 40 00:03:27,900 --> 00:03:35,400 it should try to set, if it's an equality filter, it should try to set or it should set on the inserted 41 00:03:35,400 --> 00:03:36,060 document 42 00:03:36,060 --> 00:03:41,460 because if you looked for this, this should probably be part of the new document and that is why it also 43 00:03:41,460 --> 00:03:43,090 added name Maria here 44 00:03:43,140 --> 00:03:51,420 even though I only specified these three values or these three keys. So upsert can be very useful for 45 00:03:51,420 --> 00:03:58,800 working with updates where you don't know if something or if a certain document already exists.