Views视图
On this page本页内容
A MongoDB view is a read-only queryable object whose contents are defined by an aggregation pipeline on other collections or views.MongoDB视图是一个只读的可查询对象,其内容由其他集合或视图上的聚合管道定义。
MongoDB does not persist the view contents to disk. MongoDB不会将视图内容持久化到磁盘。A view's content is computed on-demand when a client queries the view.当客户端查询视图时,视图的内容是按需计算的。
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.要了解视图类型之间的差异,请参阅与按需物化视图的比较。
Use Cases使用案例
You can use views to:您可以使用视图:
Create a view on a collection of employee data to exclude any personally identifiable information (PII).创建一个关于员工数据集合的视图,以排除任何个人身份信息(PII)。Your application can query the view for employee data that does not contain any PII.您的应用程序可以在视图中查询不包含任何PII的员工数据。Create a view on a collection of sensor data to add computed fields and metrics.创建传感器数据集合的视图,以添加计算字段和度量。Your application can use您的应用程序可以使用find operations
to query the computed data.find
操作来查询计算的数据。Create a view that joins two collections containing inventory and order history.创建一个连接包含inventory
和订单历史记录的两个集合的视图。Your application can query the view without managing or understanding the underlying pipeline.您的应用程序可以在不管理或不了解底层管道的情况下查询视图。
Create and Manage Views创建和管理视图
To learn how to create and manage views, see the following pages:要了解如何创建和管理视图,请参阅以下页面:
Create and Query a View创建和查询视图Use a View to Join Two Collections使用视图联接两个集合Create a View with Default Collation创建具有默认排序规则的视图Modify a View修改视图Remove a View删除视图
Comparison with On-Demand Materialized Views与按需物化视图的比较
MongoDB provides two different view types: standard views and on-demand materialized views. Both view types return the results from an aggregation pipeline.MongoDB提供了两种不同的视图类型:标准视图和按需物化视图。这两种视图类型都从聚合管道返回结果。
Standard views are computed when you read the view, and are not stored to disk.标准视图是在读取视图时计算的,不会存储到磁盘中。On-demand materialized views are stored on and read from disk.按需物化视图存储在磁盘上并从磁盘读取。They use a它们使用$merge
or$out
stage to update the saved data.$merge
或$out
阶段来更新保存的数据。
Indexes索引
Standard views use the indexes of the underlying collection. 标准视图使用基础集合的索引。As a result, you cannot create, drop or re-build indexes on a standard view directly, nor get a list of indexes on the view.因此,您无法直接在标准视图上创建、删除或重新构建索引,也无法获取视图上的索引列表。
You can create indexes directly on on-demand materialized views because they are stored on disk.您可以直接在按需具体化视图上创建索引,因为它们存储在磁盘上。
Performance性能
On-demand materialized views provide better read performance than standard views because they are read from disk instead of computed as part of the query. 随需应变物化视图比标准视图提供更好的读取性能,因为它们是从磁盘读取的,而不是作为查询的一部分进行计算。This performance benefit increases based on the complexity of the pipeline and size of the data being aggregated.这种性能优势随着管道的复杂性和聚合数据的大小而增加。
Behavior行为
The following sections describe behavior specific to views.以下部分描述了特定于视图的行为。
Read Only只读
Views are read-only. 视图是只读的。Write operations on views return an error.对视图的写入操作返回错误。
View Pipelines查看管道
The view's underlying aggregation pipeline is subject to the 100 megabyte memory limit for blocking sort and blocking group operations.视图的底层聚合管道受阻塞排序和阻塞组操作的100兆字节内存限制。
Starting in MongoDB 6.0, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default. 从MongoDB 6.0开始,需要超过100MB内存才能执行的管道阶段默认情况下会将临时文件写入磁盘。In earlier verisons of MongoDB, you must pass 在MongoDB的早期版本中,必须将{ allowDiskUse: true }
to individual find
and aggregate
commands to enable this behavior.{ allowDiskUse: true }
传递给单个find
和aggregate
命令才能启用此行为。
Individual 单独的find
and aggregate
commands may override the allowDiskUseByDefault
parameter by either:find
和aggregate
命令可以通过以下方式覆盖allowDiskUseByDefault
参数:
Using当{ allowDiskUse: true }
to allow writing temporary files out to disk whenallowDiskUseByDefault
is set tofalse
allowDiskUseByDefault
设置为false
时,使用{ allowDiskUse: true }
允许将临时文件写入磁盘Using当{ allowDiskUse: false }
to prohibit writing temporary files out to disk whenallowDiskUseByDefault
is set totrue
allowDiskUseByDefault
设置为true
时,使用{ allowDiskUse: false }
禁止将临时文件写入磁盘
Sharded Views分片视图
Views are considered sharded if their underlying collection is sharded. 如果视图的基础集合是分片的,则视图被视为分片的。You cannot specify a sharded view for the 在from
field in $lookup
and $graphLookup
operations.$lookup
和$graphLookup
操作中,不能为from
字段指定分片视图。
Time Series Collections时间序列集合
Time series collections are writable non-materialized views.时间序列集合是可写的非物化视图。Limitations for views apply to time series collections.视图的限制适用于时间序列集合。For more information, see Time Series Collection Limitations.有关详细信息,请参阅时间序列采集限制。You cannot create a view from a time series bucket collection namespace (namely, a collection prefixed with不能从时间序列bucket集合命名空间(即以system.buckets
).system.buckets
为前缀的集合)创建视图。
Do not attempt to create a time series collection or view with the name 请勿尝试创建名为system.profile
. system.profile
的时间序列集合或视图。MongoDB 6.3 and later versions return an 如果您尝试这样做,MongoDB 6.3及更高版本会返回一个IllegalOperation
error if you attempt to do so. IllegalOperation
错误。Earlier MongoDB versions crash.早期的MongoDB版本崩溃。
Access Control访问控制
If the deployment enforces authentication, 如果部署强制进行身份验证,则db.createView()
requires that the authenticated user have the createCollection
privilege on the database.db.createView()
要求经过身份验证的用户对数据库具有createCollection
权限。
However, if the user has the 但是,如果用户在数据库上有createCollection
on the database and find
on the view to create, the user must also have the following additional permissions:createCollection
,并且在视图上有find
要创建,则用户还必须具有以下附加权限:
源集合或视图上的find
on the source collection or view.find
。在find
on any other collections or views referenced in thepipeline
, if any.pipeline
中引用的任何其他集合或视图上find
(如果有的话)。
A user with the 在数据库中具有内置readWrite
built in role on the database has the required privileges to run the listed operations. readWrite
角色的用户具有运行所列操作所需的权限。Either create a user with the required role or grant the role to an existing user创建具有所需角色的用户,或将角色授予现有用户