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:

Modify View in MongoDB Method 2

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.

Similar Reads

Views in MongoDB

In SQL, views are referred to as virtual tables, similarly in MongoDB views are referred to as virtual collections. Views in MongoDB are implemented using an aggregation pipeline. We can create a view using a field from one or more collections in the database....

Why are Views Useful?

View provides the specific data according to the aggregation pipeline. They are used to hide the important details of the documents and provide the necessary fields from the document. It is used to restrict access to certain fields from the document....

How do Views Relate to Data Aggregation?

Views in MongoDB are the virtual collection. Data aggregation is an important factor in creating a view. View represents the aggregated data from the collection....

Creating our First View

Views in MongoDB can be created using db.createCollection() or db.createView( ). To create a view, a collection with documents is required....

How to Drop a View in MongoDB?

View are read only, standard view are not stored in the database. As the task is complete view are dropped. drop() method is used to drop the view....

How to Duplicate a View?

Duplicate view contains the same documents as the original view. Duplicate view is created using createView() method....

Modify View in MongoDB- Method 1

Drop and Recreate the View...

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....

Use a View to Join Two Collections

View are used to join two collections .createView() is used to join the two collections....

Standard Views vs On-demand Materialized Views in MongoDB

Standard Views On-demand materialized views They are read-only and are not stored on the disk They are stored on a disk. They use the indexes of the original collection. An index can be directly created. They require processing time to display views. They are pre-computed hence offer fast retrieval They don’t require maintenance They require maintenance when there are updates in collection....

Conclusion

View are used to represent the data and they are not stored in database.They are used for abstraction,enforcing security and for restricting some fields from the document. The article covers detailed information about the view with suitable example. View in mongoDB are the virtual collection. They are used to represent the data.They are read only ,i.e any modification to it is not integrated with the original collection or database. Indexes of the original collections are used by View....