Navigation

Views

A MongoDB view is a read-only queryable object whose contents are defined by an aggregation pipeline on other collections or views.

MongoDB does not persist the view contents to disk. A view’s content is computed on-demand when a client queries the view.

Note

Disambiguation

This page discusses standard views. For discussion of on-demand materialized views, see On-Demand Materialized Views.

To understand the differences between the view types, see Comparison with On-Demand Materialized Views.

Use Cases

You can use views to:

  • Create a view on a collection of employee data to exclude any personally identifiable information (PII). Your application can query the view for employee data that does not contain any PII.
  • Create a view on a collection of sensor data to add computed fields and metrics. Your application can use find operations to query the computed data.
  • Create a view that joins two collections containing inventory and order history. Your application can query the view without managing or understanding the underlying pipeline.

Comparison with On-Demand Materialized Views

MongoDB provides two different view types: standard views and on-demand materialized views. Both view types return the results from an aggregation pipeline.

  • Standard views are computed when you read the view, and are not stored to disk.
  • On-demand materialized views are stored on and read from disk. They use a $merge or $out stage to update the saved data.

Indexes

Standard views use the indexes of the underlying collection. As a result, you cannot create, drop or re-build indexes on a standard view directly, nor get a list of indexes on the view.

You can create indexes directly on on-demand materialized views because they are stored on disk.

Performance

On-demand materialized views provide better read performance than standard views because they are read from disk instead of computed as part of the query. This performance benefit increases based on the complexity of the pipeline and size of the data being aggregated.

Behavior

The following sections describe behavior specific to views.

Read Only

Views are read-only. Write operations on views return an error.

View Pipelines

The view’s underlying aggregation pipeline is subject to the 100 megabyte memory limit for blocking sort and blocking group operations.

Starting in MongoDB 6.0, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default. In earlier verisons of MongoDB, you must pass { allowDiskUse: true } to individual find and aggregate commands to enable this behavior.

Individual find and aggregate commands may override the allowDiskUseByDefault parameter by either:

  • Using { allowDiskUse: true } to allow writing temporary files out to disk when allowDiskUseByDefault is set to false
  • Using { allowDiskUse: false } to probibit writing temporary files out to disk when allowDiskUseByDefault is set to true

Sharded Views

Views are considered sharded if their underlying collection is sharded. You cannot specify a sharded view for the from field in $lookup and $graphLookup operations.

Access Control

If the deployment enforces authentication, db.createView() requires that the authenticated user have the createCollection privilege on the database.

However, if the user has the createCollection on the database and find on the view to create, the user must also have the following additional permissions:

  • find on the source collection or view.
  • find on any other collections or views referenced in the pipeline, if any.

A user with the readWrite built in role on the database has the required privileges to run the listed operations. Either create a user with the required role or grant the role to an existing user