1 00:00:02,260 --> 00:00:09,280 Let's start with documents schema and there's one important question, wasn't mongodb all about 2 00:00:09,280 --> 00:00:18,340 being schemaless and enforcing no schemas? It is, mongodb does not enforce any schemas, your documents 3 00:00:18,670 --> 00:00:26,690 can look the way you want and you can have totally different documents in one and the same collection, 4 00:00:26,710 --> 00:00:28,740 let me show you an example for that. 5 00:00:28,990 --> 00:00:35,280 Here I am again connected to my local and now cleaned up mongodb server, 6 00:00:35,320 --> 00:00:38,350 I simply deleted the data we wrote in the last lectures. 7 00:00:38,590 --> 00:00:44,590 So I got a clean database here, a clean server and I will simply switch to a new database which as you 8 00:00:44,590 --> 00:00:47,650 learned will be created on the fly, on demand. 9 00:00:47,950 --> 00:00:54,430 So I will switch to a new database and I will simply name it shop because imagine we're building the 10 00:00:54,430 --> 00:00:59,290 backend for our online shop and we're now modeling the database for it. 11 00:00:59,290 --> 00:01:05,110 Now in that database, I'll probably have a product collection where I store my product data and there 12 00:01:05,170 --> 00:01:14,560 I will insert one product, a new document therefore with let's say a name and I'll simply name it a book 13 00:01:15,070 --> 00:01:18,300 and a price and the price might be a double, 14 00:01:18,340 --> 00:01:21,230 so a number with a decimal places. 15 00:01:21,670 --> 00:01:28,200 If I now hit enter, this gets acknowledged, this gets inserted and it gets this automatically created 16 00:01:28,210 --> 00:01:32,410 unique _ID field which every document requires 17 00:01:32,410 --> 00:01:34,350 and now we obviously have it in there. 18 00:01:34,360 --> 00:01:41,650 Now let's add another product and what we can do in mongodb is that this product looks totally different, 19 00:01:41,710 --> 00:01:52,160 so there I might have a title which is T-shirt and I might have a seller which is a nested document 20 00:01:52,170 --> 00:01:58,280 let's say, an embedded document with the name of Max and an age of 29. 21 00:01:58,320 --> 00:02:00,380 Now this is a totally different document, 22 00:02:00,390 --> 00:02:06,210 it still could describe a product, though the price is missing but maybe it's free, we don't know 23 00:02:06,450 --> 00:02:13,800 and if I now look into my products here by simply finding all products and pretty printing them, we see 24 00:02:13,800 --> 00:02:17,690 we got these two products in one and the same collection and this is fine, 25 00:02:17,700 --> 00:02:19,980 mongodb will not complain about that, 26 00:02:20,010 --> 00:02:21,670 you can absolutely do that, 27 00:02:21,720 --> 00:02:28,470 you can mix totally different schemas and the schema simply means the structure of one document, 28 00:02:28,470 --> 00:02:29,820 so how does it look like, 29 00:02:29,820 --> 00:02:31,440 which fields does it have, 30 00:02:31,440 --> 00:02:36,360 which types of values do these fields have, that is what is a schema. 31 00:02:36,360 --> 00:02:41,990 So we get totally different structures or schemas as you could call it in one and the same collection 32 00:02:42,090 --> 00:02:43,880 and that is fine. 33 00:02:44,070 --> 00:02:46,420 As I mentioned, mongodb enforces no schemas, 34 00:02:46,440 --> 00:02:53,610 you can mix them but in reality, you will probably have some kind of schema because if you are building 35 00:02:53,610 --> 00:02:59,220 the backend for your online shop, it's in your interest that all your products have a price 36 00:02:59,220 --> 00:03:04,520 for example, that they all have a name which is stored in a field named name 37 00:03:04,620 --> 00:03:10,800 because if you then write some application code, let's say in PHP or in NodeJS and you retrieve your 38 00:03:10,800 --> 00:03:12,480 data from your database, 39 00:03:12,480 --> 00:03:18,630 you obviously want to cycle through that data and output it into a view which you render to the user 40 00:03:18,900 --> 00:03:21,710 and to cycle through your data and access your data, 41 00:03:21,710 --> 00:03:24,510 you need to know in which field which data is stored. 42 00:03:24,720 --> 00:03:31,770 So you will most likely have some kind of schema because you as a developer want it because your application 43 00:03:31,770 --> 00:03:32,720 needs it, 44 00:03:32,760 --> 00:03:39,780 so whilst you're not forced to have a schema, you will probably end up with some kind of structure and that 45 00:03:39,780 --> 00:03:41,300 is the important thing to understand. 46 00:03:41,310 --> 00:03:45,440 You've got the total freedom, total flexibility and if you need it, use it 47 00:03:45,540 --> 00:03:51,540 but in reality, you probably will have to think about how your data looks like because your data will 48 00:03:51,540 --> 00:03:55,530 need some kind of structure so that you could work with it efficiently.