Horje
Modify Valid or Invalid Documents in MongoDB

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.
  • requiredspecifies the attributes that must be present in the document.
  • properties‘ is object for defining each field.
  • bsonTypespecifies 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 } } )
  • updateManymethod 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.

FAQs

What is the purpose of using $jsonSchema in MongoDB?

$jsonSchema is used to define the JSON Schema for specifying validation rules and structure for a collection. It helps ensure that documents adhere to a predefined structure, including required fields and data types.

How can I identify documents that do not conform to a schema in MongoDB?

You can identify non-conforming documents using the find() method along with the $nor operator to filter out documents that fail to meet the criteria specified in the schema.

What should I do if I find documents that do not match the defined schema?

Once identified, you can update these documents using the updateMany() method to mark them as invalid or fix the issues. If necessary, you can also remove these documents using the deleteMany() method to maintain the integrity of your data.




Reffered: https://www.geeksforgeeks.org


Databases

Related
PL/SQL Packages PL/SQL Packages
MongoDB Update Operators MongoDB Update Operators
PL/SQL Comments PL/SQL Comments
MySQL WHERE Clause MySQL WHERE Clause
MariaDB GROUP BY Clause MariaDB GROUP BY Clause

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13