Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Thursday, 8 February 2024

MongoDB Data Validation: Schema Design

In MongoDB, data validation is performed at the collection level using JSON Schema. JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. MongoDB uses JSON Schema draft-07 for data validation. Here's a basic guide on schema design and data validation in MongoDB:


1. Define Your Schema: Start by defining the schema for your documents. This includes specifying the fields, their types, and any validation rules you want to enforce.


2. Use JSON Schema: MongoDB uses JSON Schema to define validation rules. You can specify JSON Schema validation rules when creating or updating a collection.


3. Specify Validation Rules: Specify validation rules such as required fields, data types, minimum and maximum values, regular expressions for string fields, etc.


4. Custom Validators: MongoDB also supports custom validation rules using JavaScript functions. You can write custom validation logic to enforce complex rules that cannot be expressed using JSON Schema alone.


5. Indexing: Consider creating indexes on fields that you frequently query on to improve the performance of your queries.


Here's an example of how you can define a schema and apply validation rules in MongoDB:



{

   "bsonType": "object",

   "required": ["name", "email", "age"],

   "properties": {

      "name": {

         "bsonType": "string",

         "description": "must be a string and is required"

      },

      "email": {

         "bsonType": "string",

         "description": "must be a string and is required"

      },

      "age": {

         "bsonType": "int",

         "minimum": 18,

         "maximum": 120,

         "description": "must be an integer in [18, 120] and is required"

      },

      "address": {

         "bsonType": "object",

         "properties": {

            "street": {

               "bsonType": "string",

               "description": "must be a string"

            },

            "city": {

               "bsonType": "string",

               "description": "must be a string"

            },

            "zip": {

               "bsonType": "string",

               "pattern": "\\d{5}",

               "description": "must be a string of exactly 5 digits"

            }

         }

      }

   }

}



You can then apply this schema to a collection in MongoDB using the `validator` option when creating the collection or updating it later.


javascript

db.createCollection("users", {

   validator: {

      jsonSchema: {

         // Your JSON Schema here

      }

   }

})



Or you can update an existing collection with validation rules using `collMod` command:


javascript

db.runCommand({

   collMod: "users",

   validator: {

      jsonSchema: {

         // Your JSON Schema here

      }

   }

})

Remember to carefully design your schema and validation rules according to your application requirements to ensure data consistency and integrity.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.