1 00:00:02,030 --> 00:00:03,950 So we want to add validation 2 00:00:03,950 --> 00:00:07,760 and actually I want to use my posts example here. 3 00:00:07,820 --> 00:00:11,740 Now how can you add validation in mongodb? 4 00:00:12,050 --> 00:00:17,180 A very easy way of adding it is when you add a collection for the first time, 5 00:00:17,180 --> 00:00:21,830 so when you create a collection and you learned that the collection is created automatically when you 6 00:00:21,830 --> 00:00:24,270 start inserting data into it. 7 00:00:24,350 --> 00:00:28,490 So here let's say I want to drop my posts collection, 8 00:00:28,480 --> 00:00:33,870 so now if I try to find one post, that will not work. 9 00:00:34,060 --> 00:00:42,190 Now let's create the collection differently, not implicitly once we add something but explicitly. For 10 00:00:42,190 --> 00:00:44,350 that we got the create collection command, 11 00:00:44,350 --> 00:00:51,370 we didn't use it thus far because often that lazy creation where it only gets created if it's needed 12 00:00:51,490 --> 00:00:55,440 is ok but if you want to configure your collection in a special way, 13 00:00:55,570 --> 00:00:59,070 well then you can use that create collection command. 14 00:00:59,350 --> 00:01:05,220 There you first of all as a first argument to the create collection method define the name of the collection and 15 00:01:05,260 --> 00:01:11,700 in our case, that would be posts, so previously you just accessed it and started working with it, 16 00:01:11,700 --> 00:01:15,480 now you create it and therefore you have to define the name here. 17 00:01:15,480 --> 00:01:19,850 The second argument is a document where you can configure that collection 18 00:01:19,860 --> 00:01:23,640 and one important piece of configuration here is the validator. 19 00:01:23,790 --> 00:01:31,650 The validator key takes another sub-document where you can now define a schema against which incoming 20 00:01:31,650 --> 00:01:36,010 data with insert or with inserts or updates has to validate. 21 00:01:37,540 --> 00:01:40,080 You do this by adding a json schema key, 22 00:01:40,100 --> 00:01:42,610 $jsonschema key here, 23 00:01:42,700 --> 00:01:44,540 $jsonschema 24 00:01:44,710 --> 00:01:47,860 and then again another nested document and this now holds the schema. 25 00:01:47,860 --> 00:01:51,900 So here we're just saying hey our validator is the json schema, 26 00:01:51,910 --> 00:01:58,840 historically there were other validators but it is strongly recommended to go with json schema now and 27 00:01:58,840 --> 00:02:02,740 then you define the schema here in this nested document. 28 00:02:02,750 --> 00:02:10,510 Now there you can define a schema for every document which is added to the collection, 29 00:02:10,520 --> 00:02:13,630 for example we can set the bson type here to object, 30 00:02:13,700 --> 00:02:18,810 so everything that gets added to the collection should be a valid document or object. 31 00:02:19,280 --> 00:02:26,030 More interestingly, we can set a required key here which is an array and here, we can define names of 32 00:02:26,030 --> 00:02:32,810 fields in the document which will be part of that post collection that are absolutely required and if 33 00:02:32,810 --> 00:02:40,000 we try to add data that does not have these fields, we'll get an error or warning depending on our settings. 34 00:02:40,070 --> 00:02:45,830 So here, we simply pass the names of the fields like in our case for the post here, you could say a title is 35 00:02:45,830 --> 00:02:48,140 required and a text is required, 36 00:02:48,140 --> 00:02:50,980 maybe tags are not required. 37 00:02:50,990 --> 00:02:52,730 The creator is required though, 38 00:02:52,790 --> 00:02:57,620 so creator and the comments let's say are also required. 39 00:02:57,830 --> 00:03:03,950 So now these four fields are required on every document that gets added to the post collection. 40 00:03:05,780 --> 00:03:10,760 Now we can even dive a bit deeper and start working on our properties, 41 00:03:10,850 --> 00:03:13,000 now since this actually gets a bit hard to read, 42 00:03:13,250 --> 00:03:20,020 let me actually take the entire command here and move into a text editor where that is easier to read. 43 00:03:20,030 --> 00:03:22,670 I created a validation.js file, 44 00:03:22,670 --> 00:03:24,200 it should be a js file 45 00:03:24,200 --> 00:03:28,670 if you're using a tool like visual studio code to get nice syntax completion, 46 00:03:28,700 --> 00:03:33,800 the name does not matter at all and I'll paste in my command and use the auto formatting capabilities 47 00:03:33,860 --> 00:03:42,680 of that IDE which you can find by inspecting the keybindings there under preferences keyboard shortcuts 48 00:03:43,280 --> 00:03:47,170 and then format documents is what you want to look for, this shortcut here 49 00:03:47,360 --> 00:03:50,010 I use that to format this in a nicer way. 50 00:03:52,650 --> 00:03:57,900 So this is what you wrote thus far, a validator with a schema for the overall document 51 00:03:57,900 --> 00:04:04,620 and now we can add a properties key which is another nested document where we can define for every property 52 00:04:04,620 --> 00:04:09,510 of every document that gets added to the post collection, how it should look like, 53 00:04:09,630 --> 00:04:13,500 for example the title field. 54 00:04:13,620 --> 00:04:16,700 Keep in mind, the title is required here, by the way 55 00:04:16,800 --> 00:04:17,570 that is incorrect, 56 00:04:17,580 --> 00:04:23,730 every element here should be a separate string, not one big string, so like this, it should look like this 57 00:04:23,760 --> 00:04:26,410 for strings. So title is required 58 00:04:26,460 --> 00:04:29,810 and now we can define it in even more detail, 59 00:04:29,850 --> 00:04:33,510 we can not just say it's required which is already done here, 60 00:04:33,540 --> 00:04:40,480 we can say the bson type here should be string and that comes back to the data types you learned before 61 00:04:40,630 --> 00:04:46,680 and also consult that lecture I added earlier where you see a list of all the data types because you'll 62 00:04:46,680 --> 00:04:50,720 need the type as it's noted in the mongodb documentation here, 63 00:04:50,940 --> 00:04:58,790 so only strings are accepted as titles. You can also add a description here, so it must be a string and is required 64 00:04:58,800 --> 00:05:01,970 could be added here. And we can do that 65 00:05:01,980 --> 00:05:04,130 for all our properties now, 66 00:05:04,130 --> 00:05:06,920 so for the text too, what would that be? 67 00:05:06,980 --> 00:05:12,950 Well it would also be a string and we can also pass a description here which is just the same description 68 00:05:12,980 --> 00:05:13,820 as up there. 69 00:05:16,550 --> 00:05:18,910 The creator on the other hand, 70 00:05:19,190 --> 00:05:22,850 that would be a bson type of objectId 71 00:05:22,880 --> 00:05:29,420 and here the mongodb equivalent is objectId written this as a string, 72 00:05:29,470 --> 00:05:34,880 so here we can also add this must be an objectId and is required. 73 00:05:35,880 --> 00:05:43,740 We also have our comments here and the comment has a bson type of array because in our post, we got 74 00:05:43,740 --> 00:05:47,100 multiple comments so we can also add a description, 75 00:05:47,100 --> 00:05:58,040 must be an array and of course our comments since we have multiple items in there, you can an add items key here 76 00:05:58,790 --> 00:06:02,890 which describes how the items should look like in that array. 77 00:06:03,250 --> 00:06:10,130 There you can set the bson type to object because every item will have to be a document, it's not allowed 78 00:06:10,130 --> 00:06:12,450 to be a string, it will have to be a document. 79 00:06:12,830 --> 00:06:23,710 Well and in our comments, as we added them before, we had a text and an author, so we'll again add a properties 80 00:06:23,710 --> 00:06:31,560 key here and now basically say well every item in an array has a text field which in turn is of type 81 00:06:31,890 --> 00:06:38,250 or bson type text where we can have description must be a string and is required and for it to be 82 00:06:38,250 --> 00:06:45,860 required on the items in the array, we should add the required key and say that text is required 83 00:06:45,910 --> 00:06:48,410 and that author is required 84 00:06:49,250 --> 00:06:55,060 and then down there in the properties set up, we can also add or we should add the author and say well 85 00:06:55,060 --> 00:07:07,070 the author, here the bson type is objectId and we also have a description must be an objectId and 86 00:07:07,080 --> 00:07:08,010 is required. 87 00:07:08,020 --> 00:07:12,300 Now you can find this document attached to this video in case this was too fast, 88 00:07:12,330 --> 00:07:19,060 in the end I'm defining a simple schema on how you can validate incoming data for our posts collection 89 00:07:19,060 --> 00:07:24,670 and I'm doing this when we create the collection, so we can copy that entire command now and move back 90 00:07:24,670 --> 00:07:25,960 to the shell 91 00:07:26,200 --> 00:07:30,820 and of course at the end of this module, you'll find a link to the detailed official docs with all validation 92 00:07:30,820 --> 00:07:31,940 options. 93 00:07:32,440 --> 00:07:38,100 So I can enter this command here and I got an error here, objectid is not understood 94 00:07:38,350 --> 00:07:39,500 and indeed that is correct, 95 00:07:39,550 --> 00:07:45,250 it should be objectId with a capital I, in the file which you download this will already be fixed. 96 00:07:45,250 --> 00:07:52,330 I fix it here manually and I also had a problem here, the bson type of text is not text but string, 97 00:07:52,540 --> 00:07:54,480 I did that correctly up here, 98 00:07:55,090 --> 00:07:56,730 simply reuse text here, 99 00:07:56,750 --> 00:08:01,240 that should be string. Again in the document you download this will be fixed. With that added, 100 00:08:01,240 --> 00:08:03,050 let's now add it here, we get 101 00:08:03,070 --> 00:08:04,130 ok 102 00:08:04,600 --> 00:08:09,050 and now if I look at the posts, there's nothing in there of course still 103 00:08:09,070 --> 00:08:12,110 but now let's insert a document. 104 00:08:12,160 --> 00:08:19,460 And for that, I'll repeat my previous insertion with title my first post, text and so on, 105 00:08:19,540 --> 00:08:21,620 if I hit enter, this succeeds, 106 00:08:21,640 --> 00:08:23,420 so now if I find one post, 107 00:08:23,440 --> 00:08:25,780 this is the post I added. 108 00:08:25,780 --> 00:08:33,910 Now let me try inserting something where my author is not an objectId let's say but a random number 109 00:08:33,940 --> 00:08:41,180 because maybe I have authors stored which I want to match by that number which fits another field. 110 00:08:41,190 --> 00:08:48,150 Now I get an error, I get an error in the end which says document failed validation and that is a pretty 111 00:08:48,150 --> 00:08:48,960 clear error 112 00:08:48,990 --> 00:08:49,570 right, 113 00:08:49,590 --> 00:08:51,900 we failed validation 114 00:08:51,990 --> 00:08:59,130 and if I now look into my posts with find to see all posts in there, we actually see indeed 115 00:08:59,130 --> 00:09:04,810 the post was not added there because we just get an error telling us that we failed.