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视图定义pipelinecannot include the$outor the$mergestage.pipeline不能包含$out或$merge阶段。This restriction also applies to embedded pipelines, such as pipelines used in此限制也适用于嵌入式管道,例如$lookupor$facetstages.$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:该方法具有以下参数:
view | ||
source | ||
pipeline |
| |
collation |
|
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.要查看视图的行为详细信息,请参阅行为。