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.MongoDB作为底层聚合管道的一部分,对视图执行读取操作。A view definition视图定义pipeline
cannot include the$out
or the$merge
stage.pipeline
不能包含$out
或$merge
阶段。This restriction also applies to embedded pipelines, such as pipelines used in此限制也适用于嵌入式管道,例如$lookup
or$facet
stages.$lookup
或$facet
阶段中使用的管道。
Syntax语法
db.createView
has the following syntax:具有以下语法:
db.createView(<view>, <source>, <pipeline>, <collation>)
The method has the following parameters:该方法具有以下参数:
view | string | |
source | string | |
pipeline | array | db.createView() pipeline to the source collection or view.pipeline 应用于source 集合或视图来创建视图。pipeline cannot include the $out or the $merge stage. pipeline 不能包含$out 或$merge 阶段。$lookup or $facet stages.$lookup 或$facet 阶段中使用的管道。db.getCollectionInfos() and explain operations on the view will include the pipeline that defines the view. db.getCollectionInfos() 和explain 对视图的操作将包括定义视图的管道。 |
collation | document | collation source is a collection, the view does not inherit the collection's collation settings.source 是集合,则视图不会继承集合的排序规则设置。source is another view, the view must specify the same collation settings.source 是另一个视图,则该视图必须指定相同的排序规则设置。collation 选项具有以下语法:collation: { locale field is mandatory; all other collation fields are optional. locale 字段是必需的;所有其他排序规则字段都是可选的。 |
The db.createView()
method wraps the following create
command operation:db.createView()
方法包装以下create
命令操作:
db.runCommand( { create: <view>, viewOn: <source>, pipeline: <pipeline>, collation: <collation> } )
View Names are Included in Collection List Output视图名称包含在集合列表输出中
Operations that list collections, such as 列出集合的操作,如db.getCollectionInfos()
and db.getCollectionNames()
, include views in their outputs.db.getCollectionInfos()
和db.getCollectionNames()
,在其输出中包含视图。
The view definition is public; i.e. 视图定义是公开的;db.getCollectionInfos()
and explain
operations on the view will include the pipeline that defines the view. db.getCollectionInfos()
和explain
对视图的操作将包括定义视图的管道。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.要查看视图的行为详细信息,请参阅行为。