Database Manual / Introduction / Databases & Collections

Views视图

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. A view's content is computed on-demand when a client queries the view.MongoDB不会将视图内容持久化到磁盘。当客户端查询视图时,会按需计算视图的内容。

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.要了解视图类型之间的差异,请参阅与按需物化视图的比较

You can create materialized views in the UI for deployments hosted in MongoDB Atlas.您可以在MongoDB Atlas中托管的部署的UI中创建物化视图

Use Cases用例

You can use views to:您可以使用视图来:

  • Create a view on a collection of employee data to exclude any personally identifiable information (PII). Your application can query the view for employee data that does not contain any PII.创建员工数据集合视图,以排除任何个人身份信息(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. Your application can query the view without managing or understanding the underlying pipeline.创建一个视图,将包含inventory(库存)和order(订单)历史的两个集合连接起来。您的应用程序可以在不管理或理解底层管道的情况下查询视图。
  • Create a MongoDB Search index on a view, which transforms documents and collections, so that you can partially index a collection, support incompatible data types or data models, and more. 在视图上创建MongoDB搜索索引,该索引转换文档和集合,以便您可以对集合进行部分索引,支持不兼容的数据类型或数据模型等。
  • Create a Vector Search index on a view, which transforms documents and collections, so that you can partially index a collection, support incompatible data types or data models, and more. 在视图上创建矢量搜索索引,该索引可以转换文档和集合,以便您可以对集合进行部分索引,支持不兼容的数据类型或数据模型等。

Create and Manage Views创建和管理视图

To learn how to create and manage views, see the following resources:要了解如何创建和管理视图,请参阅以下资源:

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阶段来更新保存的数据。

    When using $merge, you can use change streams to watch for changes on the materialized view. When using $out, you can't watch for changes on the materialized view.使用$merge时,您可以使用更改流来监视物化视图上的更改。使用$out时,您无法观察物化视图上的变化。

Indexes索引

Standard views use the indexes of the underlying collection. As a result, you cannot create, drop or re-build general indexes on a standard view directly, nor get a list of general indexes on the view.标准视图使用基础集合的索引。因此,您无法直接在标准视图上创建、删除或重新构建常规索引,也无法在视图上获取常规索引列表。

MongoDB stores search indexes and vector search indexes on disk. Accordingly, you can create MongoDB Search indexes and Vector Search indexes on compatible views that contain only the following stages:MongoDB在磁盘上存储搜索索引矢量搜索索引。因此,您可以在仅包含以下阶段的兼容视图上创建MongoDB搜索索引和矢量搜索索引:

You can also create indexes directly on on-demand materialized views because MongoDB stores those indexes on disk.您还可以直接在按需物化视图上创建索引,因为MongoDB将这些索引存储在磁盘上。

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.视图是只读的。对视图执行写入操作会返回错误。

Snapshot Isolation快照隔离

Views do not maintain timestamps of collection changes and do not support point-in-time or snapshot read isolation.视图不维护集合更改的时间戳,也不支持时间点或快照读取隔离。

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. These temporary files last for the duration of the pipeline execution and can influence storage space on your instance. In earlier versions of MongoDB, you must pass { allowDiskUse: true } to individual find and aggregate commands to enable this behavior.从MongoDB 6.0开始,默认情况下,需要超过100兆字节内存来执行将临时文件写入磁盘的流水线阶段。这些临时文件在管道执行期间持续存在,并可能影响实例上的存储空间。在早期版本的MongoDB中,您必须将{ allowDiskUse: true }传递给单个查找和聚合命令以启用此行为。

Individual find and aggregate commands can override the allowDiskUseByDefault parameter by either:单个findaggregate命令可以通过以下任一方式覆盖allowDiskUseByDefault参数:

  • Using { allowDiskUse: true } to allow writing temporary files out to disk when allowDiskUseByDefault is set to falseallowDiskUseByDefault设置为false时,使用{allowDiskUse:true}允许将临时文件写入磁盘
  • Using { allowDiskUse: false } to prohibit writing temporary files out to disk when allowDiskUseByDefault is set to trueallowDiskUseByDefault设置为true时,使用{allowDiskUse:false}禁止将临时文件写入磁盘

For MongoDB Atlas, it is recommended to configure storage auto-scaling to prevent long-running queries from filling up storage with temporary files. 对于MongoDB Atlas,建议配置存储自动扩展,以防止长时间运行的查询用临时文件填满存储。

If your Atlas cluster uses storage auto-scaling, the temporary files may cause your cluster to scale to the next storage tier.如果您的Atlas集群使用存储自动扩展,临时文件可能会导致您的集群扩展到下一个存储层。

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.时间序列集合是可写的非物化视图。视图的限制适用于时间序列集合。有关更多信息,请参阅时间序列集合限制

Access Control访问控制

If the deployment enforces authentication:如果部署强制执行身份验证

  • To create a view, you must have the createCollection privilege on the database that the view is created. Additionally, if you have the find privilege on the namespace of the view you want to create, you must also have the find privilege on the following resources:要创建视图,您必须对创建视图的数据库具有createCollection权限。此外,如果您对要创建的视图的命名空间具有查找权限,则还必须对以下资源具有查找权限:

    • The source collection or view from which the new view is created.创建新视图的源集合或视图。
    • Any collections or views referenced in the view pipeline.视图管道中引用的任何集合或视图。
  • To query a view, you must have the find privilege on the view namespace. You don't need the find privilege on the source collection or any namespaces referenced in the view pipeline.要查询视图,您必须具有视图命名空间的find权限。您不需要对源集合或视图管道中引用的任何命名空间拥有find权限。

A user with the built-in readWrite role on the database has the required privileges to run the listed operations. To grant the required permissions, either:在数据库上具有内置readWrite角色的用户具有运行所列操作所需的权限。要授予所需的权限,请执行以下操作之一: