1 00:00:02,500 --> 00:00:06,660 So validation works and it didn't add our incorrect post, 2 00:00:06,670 --> 00:00:11,960 now let's play around with the additional settings we can set which I already showed on my slides. For 3 00:00:12,030 --> 00:00:13,670 the settings to activate, 4 00:00:13,810 --> 00:00:17,110 I would need to create my collection again and set up the validation again, 5 00:00:17,260 --> 00:00:21,920 the problem is I of course already have my validation turned on here, 6 00:00:22,030 --> 00:00:25,850 so what can I do? As a database administrator 7 00:00:26,000 --> 00:00:30,160 and this is how you would add such settings like validation, 8 00:00:30,280 --> 00:00:36,940 you can use db.runCommand in the shell here to run, well administrative command. 9 00:00:36,990 --> 00:00:39,940 You pass a document with information about the command 10 00:00:40,110 --> 00:00:48,570 and here, the command is that we set a collMod, that stands for collection modifier and we define the collection 11 00:00:48,570 --> 00:00:50,470 which we do want to modify, posts 12 00:00:50,480 --> 00:00:54,780 in my case and then we can again pass a validator. 13 00:00:55,020 --> 00:01:01,860 So now we can pass exactly what we pass to create collection, so we pass validator and then the entire 14 00:01:01,860 --> 00:01:03,720 schema below that, 15 00:01:03,720 --> 00:01:10,770 so make sure to grab that portion of the file, go back to the shell. Now important, remove the closing 16 00:01:10,770 --> 00:01:18,910 parentheses, add a comma and paste in what you just copied and then add another closing curly brace 17 00:01:19,510 --> 00:01:21,540 and a closing parentheses 18 00:01:21,670 --> 00:01:23,110 but we're not done here. 19 00:01:23,420 --> 00:01:25,450 If I hit enter like this of course, 20 00:01:25,450 --> 00:01:28,640 it works but now I just set the same validation again. 21 00:01:29,020 --> 00:01:35,810 What I instead want to do is I want to change my validation settings, so I'll add a new file 22 00:01:36,630 --> 00:01:42,660 which you also find attached to this video then in the finished form where I copy my create collection code 23 00:01:42,660 --> 00:01:43,620 into. 24 00:01:43,620 --> 00:01:49,920 Now here, we will have run command as you just saw, then we pass our opening curly brace, 25 00:01:50,040 --> 00:01:56,220 collMod then holds the name of the collection we want to edit and then I need to remove that curly 26 00:01:56,220 --> 00:01:57,970 brace here, can reformat. 27 00:01:57,990 --> 00:01:59,220 This is now the command 28 00:01:59,340 --> 00:02:01,560 and now here I got my validator 29 00:02:01,890 --> 00:02:05,940 but now, I also want to switch an additional setting. 30 00:02:05,940 --> 00:02:11,670 Now let's search for the closing parentheses of validator because we need to set our validator configuration 31 00:02:11,820 --> 00:02:13,290 as a sibling to that validator 32 00:02:13,290 --> 00:02:14,570 key here. 33 00:02:15,000 --> 00:02:22,740 So for my ID, it comes in and highlights this, after this we can add validation level to control whether 34 00:02:22,920 --> 00:02:29,330 all inserts and updates are checked or only updates to elements which were valid before 35 00:02:29,760 --> 00:02:32,040 but I want to set a validation action here. 36 00:02:32,040 --> 00:02:35,420 The default is error which blocks the action and throws an error, 37 00:02:35,670 --> 00:02:40,380 I'll set it to warn. Let's copy that entire code, 38 00:02:40,430 --> 00:02:46,690 again you'll find it attached to this lecture and execute it here, should be successful. 39 00:02:47,670 --> 00:02:54,240 And now if I again tried to insert one post with my author being well or having a value of 12, which 40 00:02:54,240 --> 00:02:58,410 is not an objectId, this actually succeeds 41 00:02:58,650 --> 00:03:04,920 and the only thing it did is it will have written a warning into our log file and the log file is stored 42 00:03:04,920 --> 00:03:05,960 on our system, 43 00:03:06,060 --> 00:03:10,570 we'll have a look at this later lecture when we talk about configuring our server and so on. 44 00:03:11,850 --> 00:03:13,270 So this is the difference, 45 00:03:13,440 --> 00:03:17,920 now what you want to use here depends on your application and your requirements there. 46 00:03:18,240 --> 00:03:23,640 It's good to know that you can configure these things and putting validation in place can make a lot 47 00:03:23,640 --> 00:03:30,450 of sense depending on how your application works, how important it is for you to have one consistent 48 00:03:30,450 --> 00:03:36,930 scheme of data and you also now saw that you can define that validation schema when you create the 49 00:03:36,930 --> 00:03:40,940 collection or also thereafter with the help of run command.