MongoDB Schema Validation Rules. How to apply schema validation rules in… | by Panos Zafeiropoulos | Mar, 2022

Easy methods to apply schema validation guidelines in a set

Notebook and pencil

MongoDB is a extremely popular free and open supply cross-platform document-oriented database. It’s a NoSQL database and it’s primarily based on JSON-like paperwork. Doc-based databases are both schema-less or they supply a sure stage of flexibility defining schemas utilizing schema validation guidelines.

For individuals who are coming from the RDBMS world, the place a desk construction is characterised by columns with strictly outlined properties (sort, measurement, and so on.,), the power to outline schemas may very well be proved to be a fairly helpful choice.

Usually, we are able to suppose {that a} MongoDB database object is just like an RDBMS schema containing tables, views, and different RDBMS objects. Respectively, a MongoDB assortment is analogous to a desk, and a MongoDB doc will be thought-about as a desk row.

A MongoDB database can group collectively collections, a set holds paperwork, and a doc consists of plenty of objects of key-value pairs, and even of different paperwork.

The aim of this submit is to show how we are able to apply some schema validation guidelines in a set. For that, it’s essential to create an instance MongoDB database with a MongoDB assortment.

Right here, you will get an outline.

It’s presumed that you’ve an obtainable and accessible operating MongoDB occasion. For those who don’t have this, then you possibly can simply obtain that by additionally utilizing Docker and the official MongoDB Docker image to run a MongoDB container. Learn extra at

For comfort, we’re additionally going to make use of the MongoDB Compass which is the official GUI for MongoDB.

You may create and run a Docker container named ‘mongodb’ by operating the next command:

docker run --name mongodb -p 27017:27017 -d mongo

After you created the container, you possibly can cease and begin it utilizing the next instructions, respectively:

docker cease mongodb
docker begin mongodb

Additionally, you possibly can at all times test the operating containers, through the next:

docker ps

You may obtain the GUI Compass on the following hyperlink:

After you put in it, run it. Be sure that the mongodb container is up and operating, and create a brand new connection utilizing a connection string, which for our case it may be the next:


Then, after you’ve gotten efficiently linked to a MongoDB docker occasion, you possibly can create a brand new database and a brand new assortment. Title them ‘ticket-management’ and ‘customers,’ respectively.

The ‘customers’ assortment will retailer consumer paperwork and the paperwork must be validated by our validation guidelines.

Outline a MongoDB doc properties

As now we have mentioned earlier than, a MongoDB doc is an ordered set of key-value pairs. A key distinction to the RDBMS is {that a} MongoDB doc can retailer paperwork of any measurement of key-value pairs in addition to nested paperwork.

Nevertheless, in our case, we wish to implement the ‘customers’ assortment to carry paperwork of strictly the identical properties (keys). That is analogous to the fields (columns) of a desk in an RDBMS. So, for instance, we wish every doc to have precisely the identical properties/fields.

The mongo _id

Earlier than we determine the fields of our ‘customers’ assortment, it’s price mentioning that MongoDB mechanically generates a particular _id property/discipline every time a brand new doc is being inserted into a set.

The _id is a particular knowledge sort for MongoDB. It’s truly a MongoDB object (ObjectID) of BSON sort with a 12-byte measurement. The 12-byte _id consists of the next:

  • 4 bytes representing the seconds for the reason that Unix epoch
  • 3 bytes particular to the host — a machine identifier
  • 2 bytes of the method id, and
  • 3 bytes representing a counter, beginning with a random worth

Even the truth that an auto-generated _id just isn’t truly a normal UUID. The _id fields can be considered unique. They’re ordered, and so they can be utilized because the ‘major key’ of our assortment.

After that, that is the instance record of the fields for the ‘customers’ assortment:

electronic mail,

The purpose is to make sure (properly, as a lot as we are able to) that each one paperwork aimed to be inserted into the ‘customers’ assortment ought to include these fields.

As a way to obtain all of the paperwork to adjust to the above fields, we’ll use a particular MongoDB schema. You may suppose {that a} MongoDB schema is nothing however a algorithm for doc properties (keys) and values. These guidelines are performing on a per-collection foundation. The principles must be adopted (=validated) throughout every doc’s insertion or replace within the particular assortment.

Such a algorithm must be outlined utilizing a JSON file in response to the BSON requirements.

We’re not going to undergo extra particulars right here, however you possibly can learn extra in regards to the MongoDB schema and the way it works utilizing the official documentation. For instance, you possibly can observe the hyperlinks beneath:

After the quick intro given above, now it’s time to outline our MongoDB validation schema. The abstract of what we truly wish to outline is given beneath:

  • The fields: username, electronic mail, and password must be current in every doc (they’re obligatory).
  • The fields: username, electronic mail, and password must be of sort string, and their strings’ size must be between the minimal and most limits.
  • The sphere electronic mail ought to adjust to a particular regex sample.
  • The sphere: registrationdate must be of sort date.
  • The fields: confirmed and canceled must be of sort bool (Boolean: true or false).
  • The fields typeid and countryid must be of sort int (integer), and their values must be between a minimal and a most quantity.

We outline our guidelines through varied strategies through mongo shell CLI or mongosh CLI, however since now we have already created our ‘customers’ assortment in Compass, utilizing the GUI of Compass appears to be the handy means.

So, choose the ‘customers’ assortment, click on the Validation tab and put your JSON schema (depart the Validation Motion and Validation Degree to ERROR and STRICT choices, respectively). Under is our instance of validation guidelines that we’ll use:

mongodb (compass) assortment validation schema

After now we have saved our Validation guidelines in Compass, we are able to use the mongosh to get a style of what they seem like. Compass supplies us with an embedded model of the mongosh CLI.

By default, the mongosh is linked to the ‘take a look at’ database, as you possibly can see above. So, swap to the tickets-management database and navigate to the validation guidelines utilizing the db.getCollectionInfos() perform:

Plainly we can’t go deeper and see/test the “properties” objects utilizing the embedded mongosh.

Nevertheless, we are able to bounce into the container shell:

docker ps
11b9a599c13e mongodb "docker-entrypoint.s…" 3 months in the past Up 4 hours>27017/tcp mongodb
. . .
docker exec -it mongodb bash

And run the mongosh from inside it utilizing these instructions:

root@11b9a599c13e:/# mongosh
Present Mongosh Log ID: 6229dc064130345cc3d542bf
Connecting to: mongodb://
Utilizing MongoDB: 5.0.5
Utilizing Mongosh: 1.1.6
For mongosh information see:
To assist enhance our merchandise, nameless utilization knowledge is collected and despatched to MongoDB periodically (
You may opt-out by operating the disableTelemetry() command.
The server generated these startup warnings when booting:
2022-03-10T05:41:44.202+00:00: Utilizing the XFS filesystem is strongly really helpful with the WiredTiger storage engine. See
2022-03-10T05:41:45.856+00:00: Entry management just isn't enabled for the database. Learn and write entry to knowledge and configuration is unrestricted
Warning: Discovered ~/.mongorc.js, however not ~/.mongoshrc.js. ~/.mongorc.js won't be loaded.
It's possible you'll wish to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.
take a look at>

Then, we are able to swap to the ticket-management database and execute the db.collectionInfos() to acquire the validation guidelines data for the ‘customers’ assortment, as you possibly can see beneath:

This time our validation guidelines are clearly offered.

Alternatively, we are able to run simply the mongo CLI (not the mongosh), proven beneath:

root@11b9a599c13e:/# mongo
MongoDB shell model v5.0.5
connecting to: mongodb://
Implicit session: session "id" : UUID("bb023c59-6160-461d-b022-4d88658fb890")
MongoDB server model: 5.0.5
Warning: the "mongo" shell has been outdated by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and shall be eliminated in
an upcoming launch.
For set up directions, see
Welcome to the MongoDB shell.
For interactive assist, sort "assist".
For extra complete documentation, see
Questions? Strive the MongoDB Developer Neighborhood Boards
The server generated these startup warnings when booting:
2022-03-10T05:41:44.202+00:00: Utilizing the XFS filesystem is strongly really helpful with the WiredTiger storage engine. See
2022-03-10T05:41:45.856+00:00: Entry management just isn't enabled for the database. Learn and write entry to knowledge and configuration is unrestricted
Allow MongoDB's free cloud-based monitoring service, which is able to then obtain and show
metrics about your deployment (disk utilization, CPU, operation statistics, and so on).
The monitoring knowledge shall be obtainable on a MongoDB web site with a novel URL accessible to you
and anybody you share the URL with. MongoDB could use this data to make product
enhancements and to recommend MongoDB merchandise and deployment choices to you.
To allow free monitoring, run the next command: db.enableFreeMonitoring()
To completely disable this reminder, run the next command: db.disableFreeMonitoring()

Word that the mongo shell is depreciated, and it has been outdated by the mongosh.

Then once more, we are able to swap to the ticket-management database and execute the db.collectionInfos() to acquire such data for the ‘customers’ assortment:

As you possibly can see above, the result’s just about the identical.

After now we have outlined our Validation guidelines, we are able to use any of the obtainable instruments (Compass GUI, mongosh CLI, mongo CLI) and take a look at in the event that they work appropriately. For that, we are able to attempt to insert some paperwork that don’t meet the validation guidelines necessities and ensure their failure. Under, there are some such examples that can be utilized by your personal as properly.

Utilizing mongosh

Let’s attempt to insert an empty doc:

Now let’s strive once more with a doc with an invalid electronic mail:

You may proceed attempting to insert paperwork with invalid values, e.g., utilizing a worth of the typeid discipline — the worth 0, as an illustration, when it must be not less than:

Utilizing Compass

Equally, attempting to insert paperwork that don’t adjust to our validation guidelines, you’ll maintain getting failure errors, resembling the next:

Utilizing MongoDB validation guidelines is sort of helpful and saves us from plenty of complications. Nevertheless, it’s not a panacea. For example disadvantage, we are able to point out the shortcoming to outline uniqueness with fields, e.g., we can’t forestall insertion (or replace) of a doc with a username worth that’s already existed in one other doc.

One other instance is that we can’t additionally forestall the insertion of paperwork that shouldn’t have all of the fields (other than the required ones). And so forth.

Nevertheless, as MongoDB suggests, such challenges will be solved in our enterprise logic in middleware, however that is the topic of one other submit. So, keep tuned!

That’s it!

Thanks for studying, and glad coding!

More Posts