Modify View in MongoDB Method- 2
View are modified using the collMod command. The runCommand() method is used to carry out the modification in the view.
Syntax:
db.runCommand( collMod : "View_name " , viewOn: "source_collection ",pipeline )
Explanation:
- runCommand() is used to carry out operations that are not included with CRUD operations.
- collMod is used to modify validation rules.
- viewOn is used to define the aggregation pipeline that define the view.
Query:
Example: Modify views to represent a teacher with less than 7 years of experience.
db.runCommand({
collMod: "ExperiencedTeacher",
viewOn: "Teacher",
pipeline: [{ $match: { year: { $gt: 7 } } }],
});
Output:
Explanation: Initially view is created which contains year field with value greater than 7. This view is modified using collMod command. This view contains year field with value less than 7.
MongoDB Views
MongoDB was released in February 2009. It is an open-source document-oriented database and is classified as a NoSQL database. It follows the CAP theorem (Consistency Availability and Partition tolerance).
MongoDB stores the records in a document in BSON format. It is an unstructured language and provides horizontal scalability and high-performance, data persistence. It follows the BASE ( Basically Available, Soft State, and Eventual Consistency )properties.
In this article, We will learn about the View in MongoDB in detail along with its syntax, examples, and so on.