So we want to add validation
and actually I want to use my posts example here.
Now how can you add validation in mongodb?
A very easy way of adding it is when you add a collection for the first time,
so when you create a collection and you learned that the collection is created automatically when you
start inserting data into it.
So here let's say I want to drop my posts collection,
so now if I try to find one post, that will not work.
Now let's create the collection differently, not implicitly once we add something but explicitly. For
that we got the create collection command,
we didn't use it thus far because often that lazy creation where it only gets created if it's needed
is ok but if you want to configure your collection in a special way,
well then you can use that create collection command.
There you first of all as a first argument to the create collection method define the name of the collection and
in our case, that would be posts, so previously you just accessed it and started working with it,
now you create it and therefore you have to define the name here.
The second argument is a document where you can configure that collection
and one important piece of configuration here is the validator.
The validator key takes another sub-document where you can now define a schema against which incoming
data with insert or with inserts or updates has to validate.
You do this by adding a json schema key,
$jsonschema key here,
$jsonschema
and then again another nested document and this now holds the schema.
So here we're just saying hey our validator is the json schema,
historically there were other validators but it is strongly recommended to go with json schema now and
then you define the schema here in this nested document.
Now there you can define a schema for every document which is added to the collection,
for example we can set the bson type here to object,
so everything that gets added to the collection should be a valid document or object.
More interestingly, we can set a required key here which is an array and here, we can define names of
fields in the document which will be part of that post collection that are absolutely required and if
we try to add data that does not have these fields, we'll get an error or warning depending on our settings.
So here, we simply pass the names of the fields like in our case for the post here, you could say a title is
required and a text is required,
maybe tags are not required.
The creator is required though,
so creator and the comments let's say are also required.
So now these four fields are required on every document that gets added to the post collection.
Now we can even dive a bit deeper and start working on our properties,
now since this actually gets a bit hard to read,
let me actually take the entire command here and move into a text editor where that is easier to read.
I created a validation.js file,
it should be a js file
if you're using a tool like visual studio code to get nice syntax completion,
the name does not matter at all and I'll paste in my command and use the auto formatting capabilities
of that IDE which you can find by inspecting the keybindings there under preferences keyboard shortcuts
and then format documents is what you want to look for, this shortcut here
I use that to format this in a nicer way.
So this is what you wrote thus far, a validator with a schema for the overall document
and now we can add a properties key which is another nested document where we can define for every property
of every document that gets added to the post collection, how it should look like,
for example the title field.
Keep in mind, the title is required here, by the way
that is incorrect,
every element here should be a separate string, not one big string, so like this, it should look like this
for strings. So title is required
and now we can define it in even more detail,
we can not just say it's required which is already done here,
we can say the bson type here should be string and that comes back to the data types you learned before
and also consult that lecture I added earlier where you see a list of all the data types because you'll
need the type as it's noted in the mongodb documentation here,
so only strings are accepted as titles. You can also add a description here, so it must be a string and is required
could be added here. And we can do that
for all our properties now,
so for the text too, what would that be?
Well it would also be a string and we can also pass a description here which is just the same description
as up there.
The creator on the other hand,
that would be a bson type of objectId
and here the mongodb equivalent is objectId written this as a string,
so here we can also add this must be an objectId and is required.
We also have our comments here and the comment has a bson type of array because in our post, we got
multiple comments so we can also add a description,
must be an array and of course our comments since we have multiple items in there, you can an add items key here
which describes how the items should look like in that array.
There you can set the bson type to object because every item will have to be a document, it's not allowed
to be a string, it will have to be a document.
Well and in our comments, as we added them before, we had a text and an author, so we'll again add a properties
key here and now basically say well every item in an array has a text field which in turn is of type
or bson type text where we can have description must be a string and is required and for it to be
required on the items in the array, we should add the required key and say that text is required
and that author is required
and then down there in the properties set up, we can also add or we should add the author and say well
the author, here the bson type is objectId and we also have a description must be an objectId and
is required.
Now you can find this document attached to this video in case this was too fast,
in the end I'm defining a simple schema on how you can validate incoming data for our posts collection
and I'm doing this when we create the collection, so we can copy that entire command now and move back
to the shell
and of course at the end of this module, you'll find a link to the detailed official docs with all validation
options.
So I can enter this command here and I got an error here, objectid is not understood
and indeed that is correct,
it should be objectId with a capital I, in the file which you download this will already be fixed.
I fix it here manually and I also had a problem here, the bson type of text is not text but string,
I did that correctly up here,
simply reuse text here,
that should be string. Again in the document you download this will be fixed. With that added,
let's now add it here, we get
ok
and now if I look at the posts, there's nothing in there of course still
but now let's insert a document.
And for that, I'll repeat my previous insertion with title my first post, text and so on,
if I hit enter, this succeeds,
so now if I find one post,
this is the post I added.
Now let me try inserting something where my author is not an objectId let's say but a random number
because maybe I have authors stored which I want to match by that number which fits another field.
Now I get an error, I get an error in the end which says document failed validation and that is a pretty
clear error
right,
we failed validation
and if I now look into my posts with find to see all posts in there, we actually see indeed
the post was not added there because we just get an error telling us that we failed.