Views视图

On this page本页内容

A MongoDB view is a 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.当客户机查询视图时,视图的内容是按需计算的。MongoDB can require clients to have permission to query the view.MongoDB可以要求客户端拥有查询视图的权限MongoDB does not support write operations against views.MongoDB不支持对视图执行写操作。

For example, you can:例如,您可以:

When clients query a view, MongoDB appends the client query to the underlying pipeline and returns the results of that combined pipeline to the client.当客户端查询视图时,MongoDB将客户机查询附加到基础管道,并将该组合管道的结果返回给客户端。MongoDB may apply aggregation pipeline optimizations to the combined pipeline.MongoDB可以将聚合管道优化应用于组合管道。

Note

The following page discusses views.下一页讨论视图。For discussion of on-demand materialized views, see On-Demand Materialized Views instead.有关按需物化视图的讨论,请参见按需物化视图

Create View创建视图

To create or define a view:要创建或定义视图,请执行以下操作:

Note

  • You must create views in the same database as the source collection.必须在与源集合相同的数据库中创建视图。
  • The view definition pipeline cannot include the $out or the $merge stage.视图定义pipeline不能包含$out$merge阶段。If the view definition includes nested pipeline (e.g. the view definition includes $lookup or $facet stage), this restriction applies to the nested pipelines as well.如果视图定义包含嵌套管道(例如,视图定义包含$lookup$facet阶段),则此限制也适用于嵌套管道。

Behavior行为

Views exhibit the following behavior:视图显示以下行为:

Read Only只读

Views are read-only; write operations on views will error.视图是只读的;对视图的写入操作将出错。

The following read operations can support views:以下读取操作可以支持视图:

Index Use and Sort Operations索引使用和排序操作

  • Views use the indexes of the underlying collection.视图使用基础集合的索引。
  • As the indexes are on the underlying collection, you cannot create, drop or re-build indexes on the view directly nor get a list of indexes on the view.由于索引位于基础集合上,因此不能直接在视图上创建、删除或重新生成索引,也不能获取视图上的索引列表。
  • Starting in MongoDB 4.4, you can specify a $natural sort when running a find command on a view.从MongoDB 4.4开始,可以在对视图运行find命令时指定$natural排序。Prior versions of MongoDB do not support $natural sort on views.MongoDB的早期版本不支持视图上的$natural排序。
  • The view’s underlying aggregation pipeline is subject to the 100 megabyte memory limit for blocking sort and blocking group operations.视图的底层聚合管道受阻塞排序和阻塞组操作的100MB内存限制的限制。Starting in MongoDB 4.4, you can issue a find command with allowDiskUse: true on the view to allow MongoDB to use temporary files for blocking sort and group operations.从MongoDB 4.4开始,您可以在视图上发出一个带有allowDiskUse: truefind命令,以允许MongoDB使用临时文件来阻止排序和组操作。

    Prior to MongoDB 4.4, only the aggregate command accepted the allowDiskUse option.在MongoDB 4.4之前,只有aggregate命令接受allowDiskUse选项。

    See also另请阅读

    For more information on blocking sort operation memory limits, see Sort Operations.有关阻止排序操作内存限制的详细信息,请参阅排序操作

Projection Restrictions投影约束

find() operations on views do not support the following projection operators:视图上的find()操作不支持以下投影运算符:

Immutable Name不可变名称

You cannot rename views.不能重命名视图。

View Creation视图创建

  • Views are computed on demand during read operations, and MongoDB executes read operations on views as part of the underlying aggregation pipeline.视图是在读取操作期间按需计算的,MongoDB作为底层聚合管道的一部分对视图执行读取操作。As such, views do not support operations such as:因此,视图不支持以下操作:
    • db.collection.mapReduce(),
    • $text operator, since $text operation in aggregation is valid only for the first stage,$text运算符,因为聚合中的$text操作仅对第一阶段有效,
    • $geoNear pipeline stage.管道阶段。
  • If the aggregation pipeline used to create the view suppresses the _id field, documents in the view do not have the _id field.如果用于创建视图的聚合管道取消了_id字段,则视图中的文档没有_id字段。

Sharded View分片视图

Views are considered sharded if their underlying collection is sharded.如果视图的基础集合是分片的,则认为视图是分片的。As such, you cannot specify a sharded view for the from field in $lookup and $graphLookup operations.因此,不能在$lookup$graphLookup操作中为from字段指定分片视图。

Views and Collation视图和排序规则

  • You can specify a default collation for a view at creation time.可以在创建时为视图指定默认排序规则If no collation is specified, the view’s default collation is the “simple” binary comparison collator.如果未指定排序规则,则视图的默认排序规则为“简单”二进制比较排序规则。That is, the view does not inherit the collection’s default collation.也就是说,视图不会继承集合的默认排序规则。
  • String comparisons on the view use the view’s default collation.视图上的字符串比较使用视图的默认排序规则。An operation that attempts to change or override a view’s default collation will fail with an error.尝试更改或重写视图默认排序规则的操作将失败并出现错误。
  • If creating a view from another view, you cannot specify a collation that differs from the source view’s collation.如果从其他视图创建视图,则不能指定与源视图排序规则不同的排序规则。
  • If performing an aggregation that involves multiple views, such as with $lookup or $graphLookup, the views must have the same collation.如果执行包含多个视图的聚合,例如使用$lookup$graphLookup,则这些视图必须具有相同的排序规则。

Public View Definition公共视图定义

Operations that lists collections, such as db.getCollectionInfos() and db.getCollectionNames(), include views in their outputs.列出集合的操作,譬如db.getCollectionInfos()db.getCollectionNames(),在它们的输出中包括了视图。

Important

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.因此,避免在视图定义中直接引用敏感字段和值。

Drop a View删除视图

To remove a view, use the db.collection.drop() method on the view.若要删除一个视图,请在视图上使用db.collection.drop()

Modify a View修改视图

You can modify a view either by dropping and recreating the view or using the collMod command.要想修改一个视图,你既可以通过删除再重新创建的方式实现 ,也可以使用collMod命令。

Supported Operations支持的操作

The following operations provide support for views, except for the restrictions mentioned in this page:以下操作提供对视图的支持,但本页中提到的限制除外:

Commands命令Methods方法
create
collMod