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可以将聚合管道优化应用于组合管道。
Note
The following page discusses views.下一页讨论视图。For discussion of on-demand materialized views, see On-Demand Materialized Views instead.有关按需物化视图的讨论,请参见按需物化视图。
To create or define a view:要创建或定义视图,请执行以下操作:
db.createCollection() method or the create command:db.createCollection方法或create命令:
db.createView() method:db.createView()方法:
Note
pipeline cannot include the $out or the $merge stage.pipeline不能包含$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排序。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选项。
See also另请阅读
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.不能重命名视图。
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字段。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(),在它们的输出中包括了视图。
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.因此,避免在视图定义中直接引用敏感字段和值。
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命令。