1 00:00:02,200 --> 00:00:06,430 We talked a lot about schemas, structures, relations. 2 00:00:06,760 --> 00:00:09,060 Now let's talk about schema validation 3 00:00:09,070 --> 00:00:12,920 and with that, I'll come back to the data type thing 4 00:00:13,010 --> 00:00:19,680 and also to our schema structure in general. You learned that mongodb is very flexible, 5 00:00:19,690 --> 00:00:24,760 you can have totally different schemas or documents in one and the same collection 6 00:00:25,000 --> 00:00:27,370 and that flexibility is a huge plus 7 00:00:27,550 --> 00:00:33,640 but sometimes you want to lock down your flexibility here, you want to get rid of it. Sometimes you need a strict 8 00:00:33,640 --> 00:00:40,180 schema because you know your application is going to fetch posts and it is going to access the title 9 00:00:40,180 --> 00:00:44,520 on each post and it does expect that each title is a string 10 00:00:44,650 --> 00:00:50,620 and for cases like this, schema validation can help you. Now what is schema validation? 11 00:00:51,130 --> 00:00:58,510 Let's say we have an insert operation going on on our collection, on our posts collection maybe and 12 00:00:58,510 --> 00:01:03,480 the same would be true for update or insertMany operations by the way, 13 00:01:03,610 --> 00:01:06,810 so we're trying to add or edit some data in a collection. 14 00:01:07,300 --> 00:01:13,630 If we add a validation schema and I will show you how you add one in the next lecture, if we added one, 15 00:01:13,900 --> 00:01:22,120 then the schema will validate or the mongodb will validate the incoming data based on the schema we 16 00:01:22,120 --> 00:01:30,980 defined and either it accepts it and then allows the write to the database or it rejects the incoming data, 17 00:01:31,030 --> 00:01:37,690 hence your database is not touched and is not changed and the user gets an error. 18 00:01:37,690 --> 00:01:45,070 To be precise, you can define schema validation in the way I'll show it in the next lecture and you can also define 19 00:01:45,400 --> 00:01:50,880 which kinds of operations you want to validate and what you want to do if validation fails 20 00:01:50,980 --> 00:01:52,780 with two settings you can add. 21 00:01:53,200 --> 00:01:56,430 You can define which document should get validated 22 00:01:56,530 --> 00:01:58,440 and what happens if it fails, 23 00:01:58,600 --> 00:02:03,490 you can either set the validation level to strict which means all inserts and updates are checked or 24 00:02:03,490 --> 00:02:04,210 to moderate 25 00:02:04,210 --> 00:02:10,820 which means all inserts are checked but updates are only checked for documents which were valid before. 26 00:02:10,930 --> 00:02:17,470 So if you had some invalid data in there because it existed before you set up a schema, then you could still 27 00:02:17,470 --> 00:02:23,010 change these documents even if they don't fit your schema. For validation action, 28 00:02:23,020 --> 00:02:29,680 you can decide whether you want to throw an error and not go on with the insert or update, so it will 29 00:02:29,680 --> 00:02:34,690 not change your database data or if you only want to log a warning and proceed, 30 00:02:34,690 --> 00:02:40,300 so then you would still write the data, you would still change the data but log a warning that it did not 31 00:02:40,300 --> 00:02:41,800 fulfill your criteria. 32 00:02:42,010 --> 00:02:45,130 And what you want to choose here really depends on your application, 33 00:02:45,400 --> 00:02:51,610 if you're ok with incorrect data ending up in your database, you just want to be aware of it or if you 34 00:02:51,610 --> 00:02:55,430 totally need to block it. Let's have a look at that in code next.