1 00:00:02,120 --> 00:00:08,250 Ok, so in the last lecture you got a brief overview over the different insert methods we have and how they work. 2 00:00:08,260 --> 00:00:14,020 I also briefly want to mention here that there are some other ways of inserting, one important alternative 3 00:00:14,020 --> 00:00:19,840 can be seen in the update module because there will have an option to either update a document or insert 4 00:00:19,870 --> 00:00:21,660 it if it doesn't exist yet 5 00:00:21,880 --> 00:00:26,120 but this is it from the insert method world for now. 6 00:00:26,380 --> 00:00:33,790 Now the more interesting part is now what you can do with these insert methods and there, you can actually 7 00:00:33,790 --> 00:00:38,030 define or specifies some additional information. 8 00:00:38,350 --> 00:00:44,020 Let's say we have our hobbies collection where we keep track of all the hobbies people could possibly 9 00:00:44,020 --> 00:00:49,270 have and we insert many hobbies here. 10 00:00:49,300 --> 00:00:57,850 Now each hobby is a document with the name of the hobby, like sports and we can have multiple such names, 11 00:00:58,690 --> 00:01:03,960 cooking and then here also cars, 12 00:01:04,230 --> 00:01:07,110 obviously it could hit enter now and it would work. 13 00:01:07,200 --> 00:01:12,270 Now I did mention that we get this auto-generated ID, right? 14 00:01:12,340 --> 00:01:16,960 Often you want to use that ID but it also is the case that you want to use your own ID, 15 00:01:17,160 --> 00:01:22,770 maybe because the data is fetched from some other database where you already had an ID or maybe because 16 00:01:22,770 --> 00:01:28,440 you need a shorter ID, here for example you could say the name of the sport also makes up for a great 17 00:01:28,440 --> 00:01:29,010 ID. 18 00:01:29,250 --> 00:01:35,060 So we could have _id sports here because we know that this will be unique, 19 00:01:35,080 --> 00:01:43,090 we will not have sports twice in this collection, so we can absolutely use sports as an ID here and the 20 00:01:43,090 --> 00:01:49,000 same here for cooking, we can of course also use cooking and I just name it all lowercase, 21 00:01:49,000 --> 00:01:52,990 you could absolutely use a capital C in there, that would be accepted, 22 00:01:52,990 --> 00:01:57,130 so this is not required to be lowercase but I'll use it like this 23 00:01:57,460 --> 00:02:00,160 and the same for my last document, for the cars, 24 00:02:00,160 --> 00:02:04,350 I'll set _id to cars. Now important, 25 00:02:04,450 --> 00:02:07,820 It has to be _id, just ID will not work, 26 00:02:07,840 --> 00:02:11,120 it has to be _id. If I now hit enter, 27 00:02:11,170 --> 00:02:17,960 you'll see these were the inserted IDs and indeed when we look into the database, we'll see no other IDs, 28 00:02:17,980 --> 00:02:19,370 so no object ID 29 00:02:19,480 --> 00:02:22,380 and this is perfectly fine and this is not bad to do, 30 00:02:22,390 --> 00:02:26,340 absolutely not, bring your own IDs if you have them, 31 00:02:26,350 --> 00:02:30,330 so now I got these three entries in there and I got my own ID. 32 00:02:30,490 --> 00:02:33,310 Now let's say I repeat that operation, 33 00:02:33,310 --> 00:02:38,740 so I add more hobbies, I add let's say yoga here 34 00:02:41,650 --> 00:02:52,930 and then let's leave the middle one untouched, I also add let's say hiking here. Now the middle one, cooking 35 00:02:53,260 --> 00:02:59,350 is untouched because let's say the process where we add this, maybe it's user added data doesn't guarantee 36 00:02:59,350 --> 00:03:03,100 that we check whether this item exists in the database before, 37 00:03:03,160 --> 00:03:08,170 so I try to enter data with an ID that already exists in the database. 38 00:03:08,290 --> 00:03:10,260 What happens if I now hit enter? 39 00:03:10,690 --> 00:03:13,270 Well if I do this, I get an error, 40 00:03:13,270 --> 00:03:14,460 now let's see what we got. 41 00:03:15,830 --> 00:03:21,370 I get an error, a write error at item 1, arrays start with index 0, 42 00:03:21,380 --> 00:03:26,680 so it tells us that the element at index 0, the first element succeeded but at index 1, 43 00:03:26,720 --> 00:03:33,440 so the second element in the array it failed. So we failed in that bulk operation and it gives us some 44 00:03:33,440 --> 00:03:34,320 more information, 45 00:03:34,340 --> 00:03:41,300 it tells us again where it failed and that it failed because of a duplicate key error collection. 46 00:03:41,300 --> 00:03:47,300 It could have also failed due to a different error, maybe our server went down during our operation, 47 00:03:47,360 --> 00:03:48,800 that could have been a thing, 48 00:03:48,800 --> 00:03:52,450 now here it failed due to duplicate key problems, 49 00:03:52,460 --> 00:03:54,390 it also tells us the ID, cooking, 50 00:03:54,410 --> 00:03:56,970 so it gives us the exact document on which it failed. 51 00:03:57,230 --> 00:04:03,200 So this is of course helpful but you also see here that one element was inserted 52 00:04:03,830 --> 00:04:13,580 and indeed if I find, whoops, if I reach out to my hobbies and I find them all and pretty print them, you see sports 53 00:04:13,580 --> 00:04:14,270 was added, 54 00:04:14,300 --> 00:04:15,200 cooking was added, 55 00:04:15,230 --> 00:04:15,980 cars was added, 56 00:04:15,980 --> 00:04:17,590 that was my first ride 57 00:04:17,750 --> 00:04:25,220 but in the last ride where I got the error, yoga which was the first document was added to the collection, 58 00:04:25,730 --> 00:04:28,940 so it failed after this element. 59 00:04:28,940 --> 00:04:32,490 Now this is the default behavior of mongodb 60 00:04:32,900 --> 00:04:39,140 and this is called an ordered insert and ordered inserts simply means that every element you insert 61 00:04:39,380 --> 00:04:41,270 is processed standalone 62 00:04:41,330 --> 00:04:45,580 but if one fails, it cancels the entire insert operation 63 00:04:45,590 --> 00:04:49,490 but it does not rollback the elements it already inserted, 64 00:04:49,490 --> 00:04:54,950 that's important but it cancels the operation and does not continue with the next document which as 65 00:04:54,950 --> 00:04:56,560 we know would have succeeded. 66 00:04:57,990 --> 00:05:00,920 So this is the default behavior, often 67 00:05:01,060 --> 00:05:03,670 you want that behavior, sometimes you don't 68 00:05:03,720 --> 00:05:06,950 and for these cases, you can override the behavior. 69 00:05:06,960 --> 00:05:10,760 Now if I repeat that same operation, it will again fail 70 00:05:10,770 --> 00:05:17,040 but now already on the first document because yoga was inserted in the last step, now it will of course 71 00:05:17,040 --> 00:05:19,760 fail because now it's already in the database. 72 00:05:19,830 --> 00:05:22,430 So how can I change this behavior? 73 00:05:22,440 --> 00:05:29,280 Well we can pass a second argument separated with a comma to insert many, that argument is a document 74 00:05:29,280 --> 00:05:32,140 and this is now not a document that will get inserted 75 00:05:32,280 --> 00:05:37,300 instead it is a document that configures this operation. 76 00:05:37,380 --> 00:05:45,120 And here we can set some options and the option that helps us in this case here is the ordered option, 77 00:05:45,360 --> 00:05:52,870 the ordered option allows us to specify whether mongodb should perform an ordered insert which is 78 00:05:52,870 --> 00:05:53,840 the default, 79 00:05:53,910 --> 00:05:58,640 you can set this to true but that is redundant because data is the default setting, 80 00:05:58,650 --> 00:06:02,070 so now it will behave in exactly the way we saw it before 81 00:06:02,550 --> 00:06:08,070 but we can also set this to false and now it will not be an ordered insert 82 00:06:08,070 --> 00:06:10,860 Now if I now hit enter, I still get that error 83 00:06:10,920 --> 00:06:14,740 but you'll see I now get a list of all the right errors, 84 00:06:14,850 --> 00:06:20,000 yoga and cooking which already tells us that it continued after failing on yoga 85 00:06:20,370 --> 00:06:24,940 and we also see that one element was inserted and that has to be hiking 86 00:06:24,990 --> 00:06:32,640 since we try to insert three and two of them failed. And indeed, if I now look into my database with find 87 00:06:32,790 --> 00:06:36,370 pretty, we see hiking in there. 88 00:06:36,400 --> 00:06:44,780 So by setting ordered to false here, we changed the default behavior and it is up to you what you require 89 00:06:44,780 --> 00:06:46,950 or what you want in your application. 90 00:06:46,990 --> 00:06:51,200 Be aware that it will never rollback the entire operation 91 00:06:51,370 --> 00:06:56,830 if something failed, that is something I'll cover towards the end of the course in the transactions 92 00:06:56,830 --> 00:07:01,570 module because we'll need some additional knowledge to work with such transactions. 93 00:07:01,930 --> 00:07:08,320 A rollback will not happen here but you can control whether it continues with the other documents and tries 94 00:07:08,320 --> 00:07:12,580 to insert everything which is fine or if it does not 95 00:07:12,580 --> 00:07:18,940 and being able to set this is very useful because you might have cases where you can't really control whether 96 00:07:18,970 --> 00:07:25,240 the data you are trying to insert already is in the database. And then an unordered insert with ordered 97 00:07:25,360 --> 00:07:30,130 false can be your solution because you don't care about any documents that fail, 98 00:07:30,130 --> 00:07:31,470 they already are in the database, 99 00:07:31,510 --> 00:07:32,660 that's fine for you 100 00:07:33,010 --> 00:07:36,480 and you can add everything which is not in there yet. 101 00:07:37,090 --> 00:07:43,870 So this is how you can use ordered and unordered inserts to finetune the way documents get inserted 102 00:07:44,050 --> 00:07:45,140 to your needs.