- Reference >
mongo
Shell Methods >- Database Methods >
- db.createView()
db.createView()¶
On this page
-
db.
createView
()¶ Note
The following page discusses views. For discussion of on-demand materialized views, see
$merge
instead.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.
The view definition
pipeline
cannot include the$out
or the$merge
stage. If the view definition includes nested pipeline (e.g. the view definition includes$lookup
or$facet
stage), this restriction applies to the nested pipelines as well.The
db.createView
has the following syntax:The method accepts 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 is not the full namespace of the collection or view; i.e. does not include the database name and implies the same database as the view to create. You must create views in the same database as the source collection. pipeline
array An array that consists of the aggregation pipeline stage(s).
db.createView
creates the view by applying the specifiedpipeline
to thesource
collection or view.The view definition
pipeline
cannot include the$out
or the$merge
stage. If the view definition includes nested pipeline (e.g. the view definition includes$lookup
or$facet
stage), this restriction applies to the nested pipelines as well.The view definition is public; i.e.
db.getCollectionInfos()
andexplain
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.options
document Optional. Additional options for the method. The options document contains the following option field:
Field Type Description 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
source
is a collection, the view does not inherit the collection’s collation settings.If no collation is specified, the view’s default collation is the “simple” binary comparison collator.
If the underlying
source
is another view, the view must specify the same collation settings.The collation option has the following syntax:
When specifying collation, the
locale
field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.New in version 3.4.
The
db.createView()
method wraps the followingcreate
command operation:Operations that lists collections, such as
db.getCollectionInfos()
anddb.getCollectionNames()
, includes views in their outputs.Important
The view definition is public; i.e.
db.getCollectionInfos()
andexplain
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.To remove a view, use the
drop()
method on the view.
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 cannot create, drop or re-build indexes on the view directly nor get a list of indexes on the view.
Starting in MongoDB 4.4, you can specify a
$natural
sort when running afind
command on a view. Prior versions of MongoDB do not support$natural
sort on views.The view’s underlying aggregation pipeline is subject to the 100 megabyte memory limit for blocking sort and blocking group operations. Starting in MongoDB 4.4, you can issue a
find
command withallowDiskUse: true
on the view to allow MongoDB to use temporary files for blocking sort and group operations.Prior to MongoDB 4.4, only the
aggregate
command accepted theallowDiskUse
option.See also
For more information on blocking sort operation memory limits, see
Sort Operations
.
Projection Restrictions¶
find()
operations on views do not support
the following projection
operators:
View Creation¶
- Views are computed on demand during read operations, and MongoDB
executes read operations on views as part of the underlying
aggregation pipeline. As such, views do not support operations
such as:
db.collection.mapReduce()
,$text
operator, since$text
operation in aggregation is valid only for the first stage,$geoNear
pipeline stage.
- 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 is
sharded. As such, you cannot specify a sharded view for the from
field in $lookup
and $graphLookup
operations.
Views and Collation¶
- You can specify a default collation for a view at creation time. If no collation is specified, the view’s default collation is the “simple” binary comparison collator. That is, the view does not inherit the collection’s default collation.
- String comparisons on the view use the view’s default collation. An operation that attempts to change or override a view’s default collation will fail with an error.
- If creating a view from another view, you cannot specify a collation that differs from the source view’s collation.
- If performing an aggregation that involves multiple views, such as
with
$lookup
or$graphLookup
, the views must have the same collation.
Resource Locking¶
Changed in version 4.2.
db.createView()
obtains an exclusive lock on the
specified collection or view for the duration of the operation. All
subsequent operations on the collection must wait until
db.createView()
releases the lock. db.createView()
typically holds
this lock for a short time.
Creating a view requires obtaining an additional exclusive lock
on the system.views
collection in the database. This lock blocks
creation or modification of views in the database until the command
completes.
Prior to MongoDB 4.2, db.createView()
obtained an exclusive lock
on the parent database, blocking all operations on the database and
all its collections until the operation completed.
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 thepipeline
, 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
Examples¶
Create a View from a Single Collection¶
Given a collection survey
with the following documents:
The following operation creates a managementFeedback
view with
the _id
, feedback.management
, and department
fields:
Query a View¶
To query the view, you can use db.collection.find()
on
the view:
The operation returns the following documents:
Perform Aggregation Pipeline on a View¶
The following operation performs an aggregation on the
managementFeedback
view, using the $sortByCount
to
group by the department
field and sort in descending order by the
count of each distinct department:
The operation returns the following documents:
Create a View from Multiple Collections¶
Given the following two collections:
The
orders
collection:The
inventory
collection:
The following db.createView()
example specifies a
$lookup
stage to create a view from the join of the two
collections:
Query a View¶
To query the view, you can use db.collection.find()
on
the view:
The operation returns the following documents:
Perform Aggregation Pipeline on a View¶
The following operation performs an aggregation on the orderDetails
view, using the $sortByCount
to group by the item
field and sort in descending order by the count of each distinct item:
The operation returns the following documents:
Create a View with Default Collation¶
Given the places
collection with the following document:
The following operation creates a view, specifying collation at the view level:
String comparisons on the view use the view’s default collation. For example, the following operation uses the view’s collation:
The operation returns 3
.
An operation that attempts to change or override a view’s default collation will fail with an error.
See also