Starting with version 3.4, MongoDB adds support for creating read-only views from existing collections or other views.
First, create a view
In MongoDB 3.4, the following is an introduction to creating or defining a view:
- Create command with Viewon and Pipeline attribute options (and Db.createcollection Helper):
db.runCommand( { create: <view>, viewOn: <source>, pipeline: <pipeline>} )
Or specify a default collation collation for this view:
db.runCommand ( { create: <view>, viewOn: <source>, pipeline: <pipeline>, collation: <collation> } )
- A new MONGO shell Command Db.createview ():
db.createView(<view>, <source>, <pipeline>, <collation> )
Second, the characteristics of the view/performance
The following behavior is shown on the chart:
Read-only
The view is read-only and the write operation on the view will cause an error.
The following read operations also support views:
-Db.collection.find ()
-Db.collection.findOne ()
-Db.collection.aggregate ()
-Db.collection.count ()
-Db.collection.distinct ()
Index usage and sorting operations
- The view uses the index of the underlying collection.
- Because the index is based on the underlying collection, the index cannot be created, dropped, or rebuilt directly on the view, nor can the index list be obtained on the view.
- You cannot specify $natural natural sort on the view
Projection limits
The Find () command does not support the following projection characters when it operates on a view:
- $
- $elemMatch
- $slice
- $meta
Name is not variable
You cannot rename a view.
From for notes (Wiz)
View (views MongoDB document translation and interpretation)