1 00:00:02,210 --> 00:00:08,110 For this I started my mongod server again and I connected to it again and I got nothing in there. 2 00:00:08,120 --> 00:00:11,470 Now if you have something in there and you want to get rid of that, 3 00:00:11,540 --> 00:00:17,960 you can always use a database and then simply run db drop database to get rid of the database and all 4 00:00:17,960 --> 00:00:19,130 the data that is in there 5 00:00:19,280 --> 00:00:23,880 and as you see, you can even run that on databases that don't exist and it will not complain. 6 00:00:24,170 --> 00:00:28,610 So then you should be in that same state and have absolutely no data in there except for the default 7 00:00:28,610 --> 00:00:30,540 databases which you need. 8 00:00:30,650 --> 00:00:34,650 Now with that, let's insert some persons again, 9 00:00:34,970 --> 00:00:40,360 so let's say we have a persons collection or let's first of all switch to a database, 10 00:00:40,370 --> 00:00:46,280 we could name it contact data and then there, let's use the persons collection and 11 00:00:46,380 --> 00:00:48,490 let's insert one person. 12 00:00:48,530 --> 00:00:49,940 Now you learned this before 13 00:00:49,970 --> 00:00:55,740 but in the end here, you enter one document, a document always is marked by the curly braces that's around 14 00:00:55,750 --> 00:01:00,030 it and then a document is made up of the key value pairs, 15 00:01:00,200 --> 00:01:06,200 so you have these fields like name is the field, the value for this field is Max and then the next field is 16 00:01:06,200 --> 00:01:07,960 separated with a comma like 17 00:01:07,970 --> 00:01:08,980 age 30, 18 00:01:09,020 --> 00:01:10,360 in this case this is a number 19 00:01:10,370 --> 00:01:12,540 hence we have no quotation marks around it 20 00:01:12,830 --> 00:01:16,570 and then we might also have some hobbies which could be an array, 21 00:01:16,610 --> 00:01:24,410 sports and cooking. And if I insert this, we get this auto-generated ID here which is guaranteed to be 22 00:01:24,410 --> 00:01:27,770 unique and which also has this temporal component, 23 00:01:27,770 --> 00:01:33,770 so if you have multiple documents, they are by default ordered after this ID because the ID reflects 24 00:01:33,770 --> 00:01:35,110 when they were created. 25 00:01:35,360 --> 00:01:44,260 So if I add another document here, let's say Manuel and I set the age to 31 26 00:01:44,670 --> 00:01:49,600 and I set the values here to cooking and let's say cars, then I inserted this document too, 27 00:01:49,740 --> 00:01:49,980 ok. 28 00:01:50,010 --> 00:01:51,430 So this is insert one, 29 00:01:51,660 --> 00:01:57,900 now let's insert many and for this I'll reach out to the same collection and there, I'll use insert many 30 00:01:57,990 --> 00:01:58,730 like this 31 00:01:58,770 --> 00:02:04,050 and now the important thing here is you don't enter multiple documents separated by a comma because 32 00:02:04,110 --> 00:02:07,130 you don't pass them as arguments to this method. 33 00:02:07,410 --> 00:02:14,930 Insert many does indeed have a way where it accepts some data after this comma, so a second argument and 34 00:02:14,920 --> 00:02:17,550 I will dive into that second argument in a second 35 00:02:17,700 --> 00:02:23,080 but if you want to insert multiple documents, you just have to pass an array of these documents, 36 00:02:23,190 --> 00:02:26,370 so a list of these documents, a comma separated list. 37 00:02:26,430 --> 00:02:34,440 So then we could insert Anna, age 29 and there maybe add some hobbies 38 00:02:34,510 --> 00:02:39,970 and as you learned in the schema module, you don't have to have the same structure on all documents in 39 00:02:39,970 --> 00:02:40,820 the same collection, 40 00:02:40,830 --> 00:02:42,220 this is totally optional 41 00:02:42,220 --> 00:02:48,070 so it would be totally fine if you inserted Anna without hobbies but I'll insert Anna with the hobbies 42 00:02:48,070 --> 00:02:57,910 of sports and yoga and as you see, that worked too even though it was only one person but of course the 43 00:02:57,910 --> 00:03:03,400 main idea behind insert many is that you do enter multiple persons or multiple documents but you don't 44 00:03:03,400 --> 00:03:08,170 have to as you just saw, it's perfectly fine if you use insert many with one document only, 45 00:03:08,200 --> 00:03:10,650 you still need to put it into an array, 46 00:03:10,660 --> 00:03:13,100 it does not work otherwise. 47 00:03:13,120 --> 00:03:19,740 So if I now enter more persons here real quick and I'll leave out the hobbies to speed it up for those, 48 00:03:19,750 --> 00:03:23,080 so here we also have Maria and we have Chris, 49 00:03:26,300 --> 00:03:32,460 then here you see I inserted two documents separated by a comma also enclosed in square brackets, 50 00:03:32,460 --> 00:03:35,010 hence it's an array and this also worked. 51 00:03:35,570 --> 00:03:41,600 And with that, I got all these persons in the database and these are the insert methods you learned about. 52 00:03:41,600 --> 00:03:48,080 Now as I mentioned, there also would be the insert method, the insert method can still be used, 53 00:03:48,140 --> 00:03:50,260 it's just not recommended. 54 00:03:50,330 --> 00:03:52,920 Insert works with both a single document, 55 00:03:53,060 --> 00:04:01,660 so if we insert Phil here, this does work and you see the output is a bit different, we get a different 56 00:04:02,650 --> 00:04:03,570 result, 57 00:04:03,570 --> 00:04:06,740 now this does not mean that Phil didn't get an ID. 58 00:04:06,940 --> 00:04:14,260 If we quickly have a look into persons with find and pretty, we see that Phil does have an ID, so this 59 00:04:14,260 --> 00:04:19,090 output here just does not tell us the ID which also is an advantage of insert one and many, 60 00:04:19,120 --> 00:04:23,860 they give us a different output which is a bit more meaningful or helpful because it immediately gives 61 00:04:23,860 --> 00:04:25,930 us the ID in case we want to work with it, 62 00:04:26,050 --> 00:04:31,390 we don't have to query the database just to get that ID, which by the way is a real advantage because 63 00:04:31,390 --> 00:04:37,030 if you're building an application where you insert some data into the database, then often you want to 64 00:04:37,030 --> 00:04:41,710 get that ID back of that insert operation and immediately use it in your app. 65 00:04:41,830 --> 00:04:48,190 Let's say you build a user interface where users also can delete documents or objects they created, then 66 00:04:48,190 --> 00:04:54,850 you need that ID in your UI, in your frontend application so that if the user clicks the delete button, 67 00:04:54,910 --> 00:04:57,030 you already know which ID, 68 00:04:57,070 --> 00:04:59,320 so which document with which ID to delete. 69 00:04:59,470 --> 00:05:02,970 If you have an insert operation, that does not tell you the ID, 70 00:05:02,980 --> 00:05:08,980 it basically means that you need to wait for the insert to complete just to then query all documents and 71 00:05:08,980 --> 00:05:13,190 find that ID manually, so that your frontend continues to work. 72 00:05:13,210 --> 00:05:14,560 So this is a real thing, 73 00:05:14,560 --> 00:05:15,590 this inserted ID, 74 00:05:15,640 --> 00:05:18,840 it's not just a nice benefit, it's extremely helpful. 75 00:05:19,090 --> 00:05:22,110 So this is also another reason why insert is not that great 76 00:05:22,300 --> 00:05:28,630 but the other thing of course is we can use it for both a single person as you just saw or for multiple 77 00:05:28,630 --> 00:05:30,440 persons by adding an array here. 78 00:05:30,670 --> 00:05:43,440 So if I have Sandeep here with an age of 28 and I also have Hans here with an age of 38, then this 79 00:05:43,440 --> 00:05:47,550 also works, now you see it automatically did a bulk write, 80 00:05:47,640 --> 00:05:49,250 it gives us a different output here too, 81 00:05:49,260 --> 00:05:54,930 it still doesn't tell us the ID but on the other hand, we get useless results like that it did not remove 82 00:05:54,960 --> 00:05:55,670 any elements, 83 00:05:55,680 --> 00:06:01,350 well I did not expect it to do that on insert and therefore, insert one and insert many are definitely 84 00:06:01,350 --> 00:06:07,650 more helpful both from output perspective but also from an input, from a readability and error protection 85 00:06:07,680 --> 00:06:08,440 perspective.