视图

3.4 版本新功能

本页索引

从 3.4 开始, MongoDB 添加了基于已存在的集合或者 View (视图) 创建只读的 View 支持.

创建视图

创建或者定义一个视图, MongoDB 3.4 的介绍是:

表现

视图具备以下几种表现:

只读

视图是只读的; 通过视图进行写操作会报错.

以下为支持视图的读操作:

索引使用 & 排序操作

  • 视图使用其上游集合的索引.

  • 由于索引是基于集合的, 所以你不能基于视图创建, 删除或重建索引, 也不能获取视图的索引列表.

  • 你不能指定 $naturalarrow-up-right 排序.

    例如, 下列操作是 错误的:

Project 限制

视图上的 find()arrow-up-right 方法不支持如下projectionarrow-up-right 操作:

不能改变名称

你不能重命名视图.

视图创建

分片视图

如果视图依赖的集合是分片的, 那么视图也视为分片的. 因此, 你不能指定分片视图中 $lookuparrow-up-rightfrom字段与 $graphLookuparrow-up-right 操作.

Views 与 Collation

  • You can specify a default collationarrow-up-right 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 $lookuparrow-up-right or $graphLookuparrow-up-right , the views must have the same collationarrow-up-right.

公共视图定义

列出集合的操作, 如 db.getCollectionInfos()arrow-up-rightdb.getCollectionNames()arrow-up-right, 的结果中会包括它们的视图.

IMPORTANT

视图定义是公开的; 即在视图上的 db.getCollectionInfos()arrow-up-rightexplain 操作将会包括定义视图的管道. 因此, 请避免直接引用视图定义中敏感的字段和值.

删除视图

要删除视图, 请使用视图上的 db.collection.drop()arrow-up-right 方法.

修改视图

你可以通过删除或者重建的方式修改视图, 也可以使用 collModarrow-up-right 命令.

支持操作

以下操作支持视图, 除了本文中提到的限制除外:

原文链接:https://docs.mongodb.com/v4.2/core/views/arrow-up-right

译者 :王恒

最后更新于