So validation works and it didn't add our incorrect post,
now let's play around with the additional settings we can set which I already showed on my slides. For
the settings to activate,
I would need to create my collection again and set up the validation again,
the problem is I of course already have my validation turned on here,
so what can I do? As a database administrator
and this is how you would add such settings like validation,
you can use db.runCommand in the shell here to run, well administrative command.
You pass a document with information about the command
and here, the command is that we set a collMod, that stands for collection modifier and we define the collection
which we do want to modify, posts
in my case and then we can again pass a validator.
So now we can pass exactly what we pass to create collection, so we pass validator and then the entire
schema below that,
so make sure to grab that portion of the file, go back to the shell. Now important, remove the closing
parentheses, add a comma and paste in what you just copied and then add another closing curly brace
and a closing parentheses
but we're not done here.
If I hit enter like this of course,
it works but now I just set the same validation again.
What I instead want to do is I want to change my validation settings, so I'll add a new file
which you also find attached to this video then in the finished form where I copy my create collection code
into.
Now here, we will have run command as you just saw, then we pass our opening curly brace,
collMod then holds the name of the collection we want to edit and then I need to remove that curly
brace here, can reformat.
This is now the command
and now here I got my validator
but now, I also want to switch an additional setting.
Now let's search for the closing parentheses of validator because we need to set our validator configuration
as a sibling to that validator
key here.
So for my ID, it comes in and highlights this, after this we can add validation level to control whether
all inserts and updates are checked or only updates to elements which were valid before
but I want to set a validation action here.
The default is error which blocks the action and throws an error,
I'll set it to warn. Let's copy that entire code,
again you'll find it attached to this lecture and execute it here, should be successful.
And now if I again tried to insert one post with my author being well or having a value of 12, which
is not an objectId, this actually succeeds
and the only thing it did is it will have written a warning into our log file and the log file is stored
on our system,
we'll have a look at this later lecture when we talk about configuring our server and so on.
So this is the difference,
now what you want to use here depends on your application and your requirements there.
It's good to know that you can configure these things and putting validation in place can make a lot
of sense depending on how your application works, how important it is for you to have one consistent
scheme of data and you also now saw that you can define that validation schema when you create the
collection or also thereafter with the help of run command.