Modify Valid or Invalid Documents in MongoDB
Last Updated :
27 Dec, 2023
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:
- Define a Schema Object.
- Find Documents that Match the Schema.
- Find Documents that don’t match the Schema.
- Update Documents that don’t match the Schema.
- 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.
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.
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.
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.
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] } )
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.
Share your thoughts in the comments
Please Login to comment...