Docs HomeMongoDB Manual

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:该方法具有以下参数:

Parameter参数Type类型Description描述
viewstringThe name of the view to create.要创建的视图的名称。
sourcestringThe 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.您必须在与源集合相同的数据库中创建视图。
pipelinearrayAn array that consists of the aggregation pipeline stage(s).聚合管道阶段组成的数组。 db.createView() creates the view by applying the specified pipeline to the source collection or view.通过将指定的pipeline应用于source集合或视图来创建视图。
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阶段中使用的管道。
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. 因此,请避免直接引用视图定义中的敏感字段和值。
collationdocumentOptional.可选的。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.如果底层source是集合,则视图不会继承集合的排序规则设置。
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.如果底层source是另一个视图,则该视图必须指定相同的排序规则设置。
The collation option has the following syntax: collation选项具有以下语法:
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
When specifying collation, the locale field is mandatory; all other collation fields are optional. 指定排序规则时,locale字段是必需的;所有其他排序规则字段都是可选的。For descriptions of the fields, see Collation Document.有关字段的说明,请参阅排序规则文档

The db.createView() method wraps the following create command operation:db.createView()方法包装以下create命令操作:

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.列出集合的操作,如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.要查看视图的行为详细信息,请参阅行为