Open In App

Modify Valid or Invalid Documents in MongoDB

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In MongoDB, maintaining data consistency and integrity is crucial especially when working with collections that have specific validation rules. A document is considered valid if it follows to the predefined schema or validation rules of a collection otherwise, it is invalid.

In this article, we will understand How to modify valid or invalid documents in MongoDB.

Valid Document and Invalid Document

A valid document is a document that follows specified schema or validation rules for a collection. Invalid documents do not follow specified schema or validation rules.

Validation rules can contain the specified datatype, validation level, types, expressions or actions. By using JSON schema with $jsonSchema, validation rules are set for the collection.

Invalid Document can be removed by following steps:

  1. Define a Schema Object.
  2. Find Documents that Match the Schema.
  3. Find Documents that don't match the Schema.
  4. Update Documents that don't match the Schema.
  5. Delete Documents that don't match the Schema.

Step 1: Define a Schema Object

The collection structure is checked according to the defined schema object.

Syntax:

let schema={ 
$jsonSchema: {
required:[ attributes seperated by commas],
properties:{ attribute_name:{bsonType : "Specify_type"}}}
  • '$jsonSchema' Used to define the JSON Schema to specify validation rule and structure for a collection.
  • 'required' specifies the attributes that must be present in the document.
  • 'properties' is object for defining each field.
  • 'bsonType' specifies the datatype of the field.
    DefineSchema
    Define a Schema object.

Explanation: In the above example, collection is created and some documents are entered in the collection. In the collection five object are inserted with three fields (name, age, role ) and their values. Then the object schema named as 'schema' is defined.

Step 2: Find Documents that Satisfy Validation Rule

Documents that satisfy validation rule are found using aggregation() method and match keyword.

Syntax:

db.collection_name.aggregate( [ {  $match : schema_name } ] );
  • 'aggregate( )' method is used to perform aggregation operations, group values and analyze data from the collection.
  • '$match' is used to filter the document based on the condition.
SatisfySchema
Find documents that match the Schema.

Explanation: In the above example three documents satisfy the structure defined in 'schema' .

Step 3: Find Documents that Don't Match the Schema

Documents that don't match the schema is requested using find() method and condition .

Syntax:

db.collection_name.find (  {  $nor : [schem_name]  } )
  • 'find()' method return the document.
  • '$nor' performs logical nor operation on array elements and choose the document that fail query.
Don'tSatisfySchema
Find documents that don't match the Schema.

Explanation: In the above example, according to validation rule gender datatype should be string but int was used hence they don't match the defined schema and are returned by find method.

Step 4: Update Documents that Don't Match the Schema

Documents are updated that don't match the schema using updateMany() method.In updateMany() method, first condition is mentioned and then the field to update are mentioned.

Syntax:

db.collection_name.updateMany( { $nor: [ schema ]  } ,{ $set : {  isValid : false } } )
  • 'updateMany' method contains condition and update options.It updates all the documents that match the condition.
  • '$set' is used to specify the field which is to be updated.
UpdateDocuments
Update documents that don't match the Schema.

Explanation: In the above example, two documents don't satisfy the schema are updated. 'isValid' field is set to 'false' in collection for the two documents.

Step 5: Delete Documents that Don't Match the Schema

Documents are deleted that don't match the schema using deleteMany() method. We use deleteMany() condition to delete the documents that match the specified condition in the method.

Syntax:

db.collection_name.deleteMany(  {   $nor:  [  schema_name]  } )
DeleteSchema
Delete Documents that don't match the schema.

Explanation: Two invalid documents are deleted from the employee collection as they don't satisfy the defined schema.

Conclusion

Ensuring that all documents in a MongoDB collection conform to a specified schema is vital for data consistency and integrity. By using the $jsonSchema keyword, you can define validation rules for collections and efficiently manage documents that do not comply with these rules. This includes finding, updating, and potentially removing invalid documents. Mastering these techniques allows for better data governance and quality control in your MongoDB databases.


Next Article

Similar Reads