Database Manual / Reference / mongosh Methods / Databases

db.createView() (mongosh method方法)

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阶段中使用的管道。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

db.createView has the following syntax:具有以下语法:

db.createView(<view>, <source>, <pipeline>, <collation>)

The method has the following parameters:该方法具有以下参数:

Parameter参数Type类型Description描述
viewstring字符串The name of the view to create.要创建的视图的名称。
sourcestring字符串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.要从中创建视图的源集合或视图的名称。该名称不包括数据库名称,并暗示与要创建的视图相同的数据库;它不是集合或视图的完整名称空间。您必须在与源集合相同的数据库中创建视图。
pipelinearray数组

An 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.db.createView()通过将指定的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. As such, avoid referring directly to sensitive fields and values in view definitions.视图定义是公开的;即,db.getCollectionInfos()explain对视图的操作将包括定义视图的管道。因此,避免在视图定义中直接引用敏感字段和值。

collationdocument文档

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.如果底层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: {
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. For descriptions of the fields, see Collation Document.指定排序规则时,locale字段是必填的;所有其他排序字段都是可选的。有关字段的描述,请参阅排序规则文档。

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.要查看视图的行为详细信息,请参阅行为