最后更新于
最后更新于
3.4 版本新功能
本页索引
从 3.4 开始, MongoDB 添加了基于已存在的集合或者 View (视图) 创建只读的 View 支持.
创建或者定义一个视图, MongoDB 3.4 的介绍是:
the viewOn
and pipeline
options to the existingcommand (andhelper):
or if specifying a defaultfor the view:
a newshell helper:
视图具备以下几种表现:
视图是只读的; 通过视图进行写操作会报错.
以下为支持视图的读操作:
视图使用其上游集合的索引.
由于索引是基于集合的, 所以你不能基于视图创建, 删除或重建索引, 也不能获取视图的索引列表.
例如, 下列操作是 错误的:
视图是在读操作期间根据需要实时计算的, 同时 MongoDB 基于视图的读操作是底层聚合管道 (aggregation pipeline) 的一部分. 因此, 视图不支持一下操作:
管道阶段.
如果用于创建视图的聚合管道屏蔽了 _id
字段, 那么视图中的文档也会没有 _id
字段.
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.
IMPORTANT
以下操作支持视图, 除了本文中提到的限制除外:
译者 :王恒
你不能指定 排序.
视图上的 方法不支持如下 操作:
你不能重命名.
,
操作, 因为 $text
只在聚合的第一阶段有效,
命令和
如果视图依赖的集合是分片的, 那么视图也视为分片的. 因此, 你不能指定分片视图中 的 from
字段与 操作.
You can specify a default 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.
If performing an aggregation that involves multiple views, such as with or , the views must have the same .
列出集合的操作, 如 和 , 的结果中会包括它们的视图.
视图定义是公开的; 即在视图上的 和 explain
操作将会包括定义视图的管道. 因此, 请避免直接引用视图定义中敏感的字段和值.
要删除视图, 请使用视图上的 方法.
你可以通过删除或者重建的方式修改视图, 也可以使用 命令.
原文链接: