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:例如,您可以:
exclude
any private or personal information (PII). add
computed fields and metrics. joins
two collections containing inventory and order history respectively. 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可以对组合管道应用聚合管道优化。
The following page discusses views. 下一页讨论视图。For discussion of on-demand materialized views, see On-Demand Materialized Views instead.有关按需物化视图的讨论,请参阅按需物化视图。
To create or define a view:要创建或定义视图,请执行以下操作:
Use the 使用db.createCollection()
method or the create
command:db.createCollection()
方法或create
命令:
db.createCollection( "<viewName>", { "viewOn" : "<source>", "pipeline" : [<pipeline>], "collation" : { <collation> } } )
Use the 使用db.createView()
method:db.createView()
方法:
db.createView( "<viewName>", "<source>", [<pipeline>], { "collation" : { <collation> } } )
pipeline
cannot include the $out
or the $merge
stage. $out
阶段或$merge
阶段。$lookup
or $facet
stage), this restriction applies to the nested pipelines as well.$lookup
或$facet
阶段),则此限制也适用于嵌套管道。Views exhibit the following behavior:视图显示以下行为:
Views are read-only; write operations on views will error.视图是只读的;对视图的写入操作将出错。
The following read operations can support views:以下读取操作可以支持视图:
$natural
sort when running a find
command on a view. find
命令时,可以指定$natural
排序。$natural
sort on views.$natural
排序。The view's underlying aggregation pipeline is subject to the 100 megabyte memory limit for blocking sort and blocking group operations. 视图的底层聚合管道受阻止排序和阻止组操作的100 MB内存限制的限制。Starting in MongoDB 4.4, you can issue a 从MongoDB 4.4开始,您可以在视图上发出一个带有find
command with allowDiskUse: true
on the view to allow MongoDB to use temporary files for blocking sort and group operations.allowDiskUse:true
的find
命令,以允许MongoDB使用临时文件来阻止排序和分组操作。
Prior to MongoDB 4.4, only the 在MongoDB 4.4之前,只有aggregate
command accepted the allowDiskUse
option.aggregate
命令接受allowDiskUse
选项。
For more information on blocking sort operation memory limits, see Sort Operations.有关阻止排序操作内存限制的更多信息,请参阅排序操作。
视图上的find()
operations on views do not support the following projection operators:find()
操作不支持以下投影运算符:
You cannot rename views.不能重命名视图。
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
_id
field, documents in the view do not have the _id
field._id
字段,则视图中的文档没有_id
字段。When you query a view, the:查询视图时,会显示:
filter
, projection
, sort
, skip
, limit
, and other operations for db.collection.find()
are converted to the equivalent aggregation pipeline stages.db.collection.find()
的查询filter
、projection
、sort
、skip
、limit
和其他操作将转换为等效的聚合管道阶段。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
字段指定分片视图。
$lookup
or $graphLookup
, the views must have the same collation.$lookup
或$graphLookup
,则这些视图必须具有相同的排序规则。Operations that lists collections, such as 列出集合的操作,例如db.getCollectionInfos()
and db.getCollectionNames()
, include views in their outputs.db.getCollectionInfos()
和db.getCollectionNames()
,在其输出中包括视图。
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.因此,避免直接引用视图定义中的敏感字段和值。
To remove a view, use the 要删除视图,请对该视图使用db.collection.drop()
method on the view.db.collection.drop()
方法。
You can modify a view either by dropping and recreating the view or using the 可以通过删除并重新创建视图或使用collMod
command.collMod
命令来修改视图。
The following operations provide support for views, except for the restrictions mentioned in this page:以下操作提供对视图的支持,但本页中提到的限制除外:
create | |
collMod | |