Open In App

Modify Valid or Invalid Documents in MongoDB

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB is a NoSQL database. It handles large, complex, and unstructured data with easy scalability. It is a document-oriented database that stores data in JSON Format. 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

The schema object is defined to organise the collection. Documents are categorized as valid or invalid document based on the schema object. Invalid document are updated before the deletion of the invalid document.Hence the process is completed.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads