Modify Valid or Invalid Documents in MongoDB
Last Updated :
30 Jul, 2024
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:
- 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
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.
Similar Reads
Sorting Documents in MongoDB
Sorting documents in MongoDB is the process of arranging data in a specific order based on field values, enhancing the efficiency and readability of query results. In this article, We will learn about Sorting Documents in MongoDB by understanding various examples in detail. Sorting Documents in Mong
4 min read
How to Find Documents by ID in MongoDB?
In MongoDB, finding documents by their unique identifier (ID) is a fundamental operation that allows us to retrieve specific records efficiently. Each document in a MongoDB collection has a unique _id field, which serves as the primary key. In this article, we will explore how to find documents by I
3 min read
Modify Schema Validation in MongoDB
MongoDB Validation rules are essential for defining the structure and ensuring the integrity of documents within a collection. These rules determine what constitutes valid and invalid documents and can be modified as data requirements evolve. In this article, We will learn about the Modify Schema Va
6 min read
What is the Format of Document in MongoDB?
MongoDB, which is one of the most prominent NoSQL databases, uses a document-oriented data model. A document in MongoDB is a collection of key-value pairs, where keys are strings (field names) and values can be of various data types such as strings, numbers, arrays, boolean values, dates, or even ne
4 min read
Mongoose Documents vs Models
Mongoose, a popular Node library, is widely used for interacting with MongoDB, a NoSQL database. Mongoose simplifies the process of working with MongoDB by providing an object data modeling (ODM) framework. In the Mongoose ecosystem, two key concepts are Documents and Models. In this article, we'll
4 min read
Model Relationships Between Documents in MongoDB
MongoDB as a NoSQL database, offers a flexible, document-based structure that enables us to define relationships between documents in various ways. Unlike traditional relational databases that depend on tables and foreign keys, MongoDB supports multiple ways of representing relationships, providing
5 min read
MongoDB CRUD Operations: Insert and Find Documents
MongoDB is a NoSQL database that allows for flexible and scalable data storage using a document-based model. CRUD (Create, Read, Update, Delete) operations form the backbone of any database interaction and in MongoDB, these operations are performed on documents within collections. In this article, w
3 min read
MongoDB - Query Documents using Mongo Shell
MongoDB provides you read operations to retrieve documents from the collection or query a collection for a document. You can perform read operations using the db.collection.find() method. This method selects or views the documents from the collection and returns the cursor to the selected document.
4 min read
How to Update the _id of MongoDB Document?
In MongoDB, the _id field serves as a unique identifier for each document in a collection. While MongoDB automatically generates _id values for documents upon insertion, there are scenarios where we might need to update the _id of a document. In this article, We will learn about How to update the _i
3 min read
Multi-Document Transaction in MongoDB
MongoDB is a NoSQL database known for its flexible schema and high performance. While traditional relational databases offer ACID (Atomicity, Consistency, Isolation, Durability) transactions across multiple tables, NoSQL databases like MongoDB initially lacked this feature. However, since MongoDB 4.
3 min read