Views in MongoDB

Learn via video courses
Topics Covered

Overview

MongoDB is the read-only representation of the data which represents the result of aggregation performed on the collections. Creating a MongoDB view requires view, source, pipeline and aggregation options.

What is View in MongoDB?

Views are the result of running aggregation on the collection. While views can be read-only, they can also be writable if they are based on a single collection and the pipeline used to create the view does not include any stages that modify the data. MongoDB View helps in security as it allows only users to access a particular result set by not providing access to the underlying collection.

View and Data Aggregation in MongoDB

To get the underlying documents and collections summary, we use data aggregation. The collection is used to represent these as a result. And these collections can be aggregated data, processes or the gathered database processes. We can define it as a result of lookups and grouping available in the aggregation pipeline. Views to reduce the storage requirements for volatile data and can help improve query performance. But data aggregation is best in terms of reporting past data. Data aggregation and views techniques can be wisely implemented in any interface between the application and the database. This is because it makes implementation of security easy, makes performance tuning simple, and development of the database and application decoupled by it.

Db.create.view()

MongoDB View is created by db.create.view() as the response of the aggregation pipeline applied to the source collection or view. During the read operations, views are executed on demand and they are just like the read-only collection. Views must be created in the database where the source collection is stored. outortheout or the merge stage are not included in the view definition pipeline.

Syntax

Syntax of db.create.view() is:

Parameters accepted by db.create.view() are given below:

ParameterTypeDescription
viewstringView name
sourcestringSource collection or view name from which you want to create the view
pipelinearrayAggregation pipeline stages array. The specified pipeline is applied by the db.createView() on the source view or collection to create a view.
collationdocumentIt is an optional parameter for describing the view default collation. Language-specific rules are specified by the collation for example rules for accent marks and lettercase.

Behavior

The behavior of the MongoDB view is defined by points explained below:

Read Only : We can perform write operations on the view, they are read-only. And the view will throw an error is wrote operation is performed on it.

View Pipelines : There is a memory limit of 100 MB for the blocking group and blocking sort operations on the underlying aggregation pipeline of the view.

Sharded Views : If the underlying collection of the view is sharded then the view is also considered as sharded. The sharded view can not be specified for the from the field on the graphLookupandgraphLookup and lookup operations.

Time Series Collections:

Time series collections can be considered as views that are writable and not materialized. As a result, any restrictions or limitations that apply to views also apply to time series collections.

Examples

Let us create a collection with the data to create a view for that collection.

Create a MongoDB view which will be only limited to the students of 4th year.

Here

  • fourthYears is the view name.
  • students is the name of the collection which is the basis of the view.
  • $match is an aggregation expression for matching only fourth year students from the collection.

How to Create a View?

Aggregation Pipeline Builder must be used for the view creation. The output of the final stage of the pipeline builder will become the view content. Follow the steps given below to create a view from your results of the pipeline.

  1. In the pipeline builder, create an aggregation pipeline.
  2. Click on the arrow button available on the top of the pipeline builder next to the save button.
  3. Now click on create a View.
  4. Enter the view name according to your requirements.
  5. Now click on the Create button.

How to Open a View?

MongoDB View can be opened by one of the options given below:

  • Choose the view you want to open from the collections screens. Or
  • You can also open a view by clicking the view you want to open from the left navigation section.

How to Drop a View?

Follow the steps given below to drop the MongoDB view from the database:

  1. In the left navigation, hover over the view you want to drop.
  2. Click on the ellipses button that appeared on the screen.
  3. Now select the drop option from the dropdown.
  4. Now enter the view name you want to drop in the modal.
  5. Now click on the drop collection button.

How to Duplicate a View?

MongoDB View can be duplicated to change an existing view by keeping the original view also. Follow the steps given below to duplicate a view.

  1. In the left navigation, hover over the view you want to duplicate.
  2. Click on the ellipses button that appeared on the screen.
  3. Now select the duplicate option from the dropdown.
  4. Now choose a name for the new view and enter it.
  5. Click on the duplicate button.

Modifying the Source of a View

Follow the steps given below to modify the MongoDB view source

  1. From the left navigation, select the view whose source you want to modify.
  2. Click on the Modify Source, available at the top of the view. The aggregation pipeline builder is opened by it and the pipeline used for view creation is populated by it.
  3. Change the pipeline according to your requirements.
  4. Now click on the update view option available at the top of the pipeline builder.
  5. At the top of the view, click Modify Source.

Supported Operations in MongoDB View

Views get support from the following operations:

CommandsMethods
createdb.createView()
db.createCollection()
collModdb.getCollectionNames()
db.getCollectionInfos()
db.getCollection()
find, distinct, countdb.collection.aggregate()
db.collection.find()
db.collection.findOne()
db.collection.countDocuments()
db.collection.estimatedDocumentCount()
db.collection.count()
db.collection.distinct()

Conclusion

  • MongoDB are the read-only representation of the data which represents the result of aggregation performed on the collections.
  • To get the underlying documents and collections summary, we use data aggregation.
  • MongoDB View is created by db.create.view() as the response of the aggregation pipeline applied to the source collection or view.
  • Aggregation Pipeline Builder must be used for the view creation.
  • Create, collMod, find and count are the operations supported by the MongoDB views.