Views

New in version 3.4.

MongoDB supports creating read-only views from existing collectionsor other views. For example, you can:

  • Create a view that excludes private orconfidential data from a collection of employee data.
  • Create a view that adds computed fieldsfrom a collection of metrics.
  • Create a view that joins data from twodifferent related collections.

Each view has an associatedaggregation pipeline against the sourcedata. MongoDB computes the view contents by executing the aggregationon-demand during read operations. MongoDB executes read operationson views as part of the underlying aggregation pipeline.

Note

The following page discusses views. For discussion of on-demandmaterialized views, see On-Demand Materialized Views instead.

Create View

To create or define a view, MongoDB 3.4 introduces:

  1. db.runCommand( { create: <view>, viewOn: <source>, pipeline: <pipeline> } )

or if specifying a default collation for the view:

  1. db.runCommand( { create: <view>, viewOn: <source>, pipeline: <pipeline>, collation: <collation> } )
  1. db.createView(<view>, <source>, <pipeline>, <collation> )

Note

  • You must create views in the same database as the source collection.
  • The view definition pipeline cannotinclude the $out or the $merge stage. If the view definition includesnested pipeline (e.g. the view definition includes$lookup or $facet stage), thisrestriction applies to the nested pipelinesas well.

Behavior

Views exhibit the following behavior:

Read Only

Views are read-only; write operations on views will error.

The following read operations can support views:

Index Use and Sort Operations

  • Views use the indexes of the underlying collection.

  • As the indexes are on the underlying collection, you cannotcreate, drop or re-build indexes on the view directly nor get alist of indexes on the view.

  • You cannot specify a $natural sort on a view.

For example, the following operation is invalid:

  1. db.view.find().sort({$natural: 1})

Projection Restrictions

find() operations on views do not supportthe following projectionoperators:

Immutable Name

You cannot rename views.

View Creation

  • Views are computed on demand during read operations, and MongoDBexecutes read operations on views as part of the underlyingaggregation pipeline. As such, views do not support operationssuch as:
  • If the aggregation pipeline used to create the view suppresses the_id field, documents in the view do not have the _id field.

Sharded View

Views are considered sharded if their underlying collection issharded. As such, you cannot specify a sharded view for the fromfield in $lookup and $graphLookup operations.

Views and Collation

  • You can specify a default collationfor a view at creation time. If no collation is specified, theview’s default collation is the “simple” binary comparisoncollator. That is, the view does not inherit the collection’sdefault collation.
  • String comparisons on the view use the view’s default collation.An operation that attempts to change or override a view’s defaultcollation will fail with an error.
  • If creating a view from another view, you cannot specify acollation that differs from the source view’s collation.
  • If performing an aggregation that involves multiple views, such aswith $lookup or $graphLookup, the views musthave the same collation.

Public View Definition

Operations that lists collections, such asdb.getCollectionInfos() anddb.getCollectionNames(), include views in their outputs.

Important

The view definition is public; i.e. db.getCollectionInfos()and explain operations on the view will include the pipeline thatdefines the view. As such, avoid referring directly to sensitive fieldsand values in view definitions.

Drop a View

To remove a view, use the db.collection.drop() method on theview.

Modify a View

You can modify a view either by dropping and recreating the view orusing the collMod command.

Supported Operations

The following operations provide support for views, except for therestrictions mentioned in this page:

CommandsMethods
createdb.createCollection()db.createView()
collMod
db.getCollection()db.getCollectionInfos()db.getCollectionNames()
finddistinctcountdb.collection.aggregate()db.collection.find()db.collection.findOne()db.collection.countDocuments()db.collection.estimatedDocumentCount()db.collection.count()db.collection.distinct()