db.createView()
On this page
db.createView()
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.
Creates a view as the result of the applying the specified aggregation pipeline to the source collection or view. Views act as read-only collections, and are computed on demand during read operations. You must create views in the same database as the source collection. MongoDB executes read operations on views as part of the underlying aggregation pipeline.
A view definition
pipeline
cannot include the$out
or the$merge
stage. This restriction also applies to embedded pipelines, such as pipelines used in$lookup
or$facet
stages.
Compatibility
This method is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Note
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
db.createView
has the following syntax:
db.createView(<view>, <source>, <pipeline>, <collation>)
The method has the following parameters:
Parameter | Type | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
view | string | The name of the view to create. | ||||||||||
source | string | The name of the source collection or view from which to create
the view. The name does not include the database name and implies
the same database as the view to create; it is not the full
namespace of the collection or view. You must create views in the
same database as the source collection. | ||||||||||
pipeline | array | An array that consists of the aggregation pipeline stage(s). A view definition The view definition is public; i.e. | ||||||||||
collation | document | Optional. Specifies the default collation for the view. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. If the underlying If no collation is specified, the view's default collation is the "simple" binary comparison collator. If the underlying The collation option has the following syntax:
When specifying collation, the |
The db.createView()
method wraps the following
create
command operation:
db.runCommand( { create: <view>, viewOn: <source>, pipeline: <pipeline>, collation: <collation> } )
Important
View Names are Included in Collection List Output
Operations that list collections, such as
db.getCollectionInfos()
and db.getCollectionNames()
,
include views in their outputs.
The view definition is public; i.e. db.getCollectionInfos()
and explain
operations on the view will include the pipeline that
defines the view. As such, avoid referring directly to sensitive fields
and values in view definitions.
Examples
To see examples of creating a view, see the following pages:
Behavior
To see behavioral details of views, see Behavior.