1 00:00:02,100 --> 00:00:06,150 Now that we added some data into our database in the last lecture, 2 00:00:06,300 --> 00:00:12,690 let's come back to that json thing because actually, mongodb does not use json but bson on which 3 00:00:12,690 --> 00:00:17,040 stands for binary json for storing data in your database. 4 00:00:17,040 --> 00:00:23,160 Now you don't really need to care too much about that because all you will write and see will be json code like 5 00:00:23,160 --> 00:00:23,910 we did it, 6 00:00:24,090 --> 00:00:25,430 so this is json code, 7 00:00:25,470 --> 00:00:27,060 this is what we use in the last lecture, 8 00:00:27,060 --> 00:00:30,880 this is also what we see when we retrieve data from mongodb 9 00:00:31,050 --> 00:00:35,680 but behind the scenes, mongodb actually uses bson data. 10 00:00:35,700 --> 00:00:38,020 This conversion is done by the drivers, 11 00:00:38,040 --> 00:00:42,160 so this basically takes your json code and stores it in binary data 12 00:00:42,300 --> 00:00:49,890 and this is simply done because for one, it is more efficient to store than json data, so it's faster 13 00:00:49,890 --> 00:00:55,170 and more efficient from a space, from a size perspective 14 00:00:55,290 --> 00:01:02,790 and additionally it supports additional types because for example that objectId thing we saw in the 15 00:01:02,790 --> 00:01:05,570 last lecture is not valid normal 16 00:01:05,610 --> 00:01:11,520 json. If I were to go in to my editor which is visual studio code which you don't need but this is 17 00:01:11,520 --> 00:01:13,600 simply an editor you can use for coding 18 00:01:13,950 --> 00:01:18,170 and I would manually add an ID field here 19 00:01:19,030 --> 00:01:24,300 and set this equal to type objectId with some text in there, 20 00:01:24,310 --> 00:01:28,590 you see that my editor doesn't like this because it's not valid 21 00:01:28,600 --> 00:01:31,480 json. Now it is not valid 22 00:01:31,510 --> 00:01:36,920 json but mongodb can actually understand and take this and store it in its binary data 23 00:01:37,060 --> 00:01:38,740 and there are some other types too 24 00:01:38,740 --> 00:01:45,650 like for example you got different types of numbers with decimals and very big numbers 25 00:01:45,820 --> 00:01:47,410 and these are stored in different ways 26 00:01:47,410 --> 00:01:50,190 behind the scenes, you don't need to care too much about that 27 00:01:50,290 --> 00:01:56,260 but this is something mongodb offers you, this special data type, other special data types and very 28 00:01:56,260 --> 00:01:57,130 efficient storage 29 00:01:57,130 --> 00:02:02,820 in general. We will always work with our data like this though, 30 00:02:02,830 --> 00:02:08,470 so in json format and there's one other important thing you need to understand about this data. 31 00:02:08,620 --> 00:02:13,510 If I add another piece of information into flight data with insert one, 32 00:02:13,510 --> 00:02:19,840 so if I add another document and this time I'll type it manually, when adding data like this, 33 00:02:19,840 --> 00:02:26,140 you can actually omit the quotation marks here as long as there is no whitespace in the name. 34 00:02:26,140 --> 00:02:33,280 So we could also have departure airport like this and this would still be accepted so I don't need 35 00:02:33,280 --> 00:02:36,630 to wrap the name into quotation marks here, 36 00:02:36,790 --> 00:02:42,880 you can still do it to be very clear that this is json but well we know it's json, you can use it 37 00:02:42,880 --> 00:02:48,400 like this as a shortcut, the value if it's a string still needs to be enclosed in double quotation marks though. 38 00:02:48,610 --> 00:02:56,590 So if we had Berlin Tegel as an airport here and then let's say we have the arrival airport which 39 00:02:56,590 --> 00:02:57,990 is heathrow, 40 00:02:58,090 --> 00:03:00,270 then we could write it like this. 41 00:03:00,490 --> 00:03:06,680 We can now also theoretically save it like this even though the aircraft, the distance and the information 42 00:03:06,690 --> 00:03:12,280 whether this is an intercontinental flight is missing because you can have mixed documents in the same 43 00:03:12,280 --> 00:03:17,440 collection. If you add the data like this, you still see, this is accepted 44 00:03:17,620 --> 00:03:24,860 and if I now access all my flight data and find it and pretty print it, you'll see both documents exist 45 00:03:24,860 --> 00:03:27,690 in there even though they have different schemas. 46 00:03:27,760 --> 00:03:34,970 Now we'll dive a bit deeper into schema design and how we should structure our data in a separate module 47 00:03:35,140 --> 00:03:41,200 but I also want you to take away and keep in mind that in mongodb, two documents in the same collection 48 00:03:41,380 --> 00:03:43,520 don't have to have the same schema. 49 00:03:43,570 --> 00:03:50,980 Often you will have at least some intersections or complete the equal schemas but it's not a must. 50 00:03:51,010 --> 00:03:56,650 One other thing you have to know is that this ID which is auto-generated, you don't have to use 51 00:03:56,650 --> 00:04:00,860 the auto-generated ID, you just have to ensure that you have a unique ID 52 00:04:00,970 --> 00:04:07,560 but if you can ensure this, you can also assign IDs on your own. So if I add this same entry again with 53 00:04:07,570 --> 00:04:10,190 departure airport and arrival airport, 54 00:04:10,390 --> 00:04:16,810 I can add _ID in there as an extra field and it has to be _ID, 55 00:04:16,840 --> 00:04:23,800 so not just ID, also not ID with a capital character, _id like this 56 00:04:23,800 --> 00:04:27,310 and then you can use any ID you want, a number or 57 00:04:27,370 --> 00:04:36,920 another string like txl-lhr-1. This can be done and you see this was also accepted 58 00:04:36,920 --> 00:04:39,110 and inserted with this ID, 59 00:04:39,350 --> 00:04:46,400 so if I again print all the results here, you see we got our two old results and this new one and this 60 00:04:46,400 --> 00:04:53,450 new one does now not have an objectId which was generated for us by mongodb but our own ID. If 61 00:04:53,450 --> 00:04:58,610 I try to enter the same entry again with the same ID though and that's the only thing which is the 62 00:04:58,610 --> 00:05:00,500 problem here, not the other data, 63 00:05:00,530 --> 00:05:06,560 the other data maybe equal as you can see here but if I use the same id again, we get an error 64 00:05:06,710 --> 00:05:12,730 and in the end, this error is simply telling us that we get a duplicate key error in this collection 65 00:05:13,010 --> 00:05:17,010 and that the duplicate key is the ID which is not allowed. 66 00:05:17,020 --> 00:05:21,180 So this is the error we're seeing here and this is something you should also pay close attention to.