On this page本页内容
db.
createView
()¶Note
The following page discusses views. 下一页讨论视图。For discussion of on-demand materialized views, see 有关按需物化视图的讨论,请参阅$merge
instead.$merge
。
Creates a view as the result of the applying the specified aggregation pipeline to the source collection or view. 将指定的聚合管道应用于源集合或视图后,将创建一个视图。Views act as read-only collections, and are computed on demand during read operations. 视图充当只读集合,并在读取操作期间按需计算。You must create views in the same database as the source collection. 必须在与源集合相同的数据库中创建视图。MongoDB executes read operations on views as part of the underlying aggregation pipeline.MongoDB作为底层聚合管道的一部分对视图执行读取操作。
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
阶段),则此限制也适用于嵌套管道。
The db.createView
has the following syntax:db.createView
语法如下所示:
The method accepts the following parameters:该方法接受以下参数:
view |
string | |
source |
string | |
pipeline |
array |
|
options |
document |
The options document contains the following option field:“选项”文档包含以下选项字段:
collation |
document |
|
The db.createView()
method wraps the following create
command operation:db.createView()
方法包装以下create
命令操作:
Operations that lists collections, such as 列出集合的操作,例如db.getCollectionInfos()
and db.getCollectionNames()
, includes 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 要删除视图,请对该视图使用drop()
method on the view.drop()
方法。
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()
操作不支持以下projection运算符:
db.collection.mapReduce()
,$text
operator, since $text
operation in aggregation is valid only for the first stage,$text
运算符,因为聚合中的$text
操作仅对第一阶段有效,$geoNear
pipeline stage._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.from
field in $lookup
和$graphLookup
操作中为from
字段指定分片视图。
$lookup
or $graphLookup
, the views must have the same collation.$lookup
或$graphLookup
,则这些视图必须具有相同的排序规则。Changed in version 4.2.在版本4.2中更改。
db.createView()
obtains an exclusive lock on the specified collection or view for the duration of the operation. 在操作期间获取指定集合或视图的独占锁。All subsequent operations on the collection must wait until 集合上的所有后续操作都必须等待db.createView()
releases the lock. db.createView()
释放锁。db.createView()
typically holds this lock for a short time.db.createView()
通常会在短时间内持有此锁。
Creating a view requires obtaining an additional exclusive lock on the 创建视图需要在数据库中的system.views
collection in the database. system.views
集合上获得额外的独占锁。This lock blocks creation or modification of views in the database until the command completes.此锁将阻止在数据库中创建或修改视图,直到命令完成。
Prior to MongoDB 4.2, 在MongoDB 4.2之前,db.createView()
obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.db.createView()
获得了父数据库的独占锁,阻止了数据库及其所有集合上的所有操作,直到操作完成。
If the deployment enforces authentication, 如果部署强制进行身份验证,db.createView()
requires that the authenticated user have the createCollection
privilege on the database.db.createView()
要求经过身份验证的用户对数据库具有createCollection
权限。
However, if the user has the 但是,如果用户在数据库上具有createCollection
on the database and find
on the view to create, the user must also have the following additional permissions:createCollection
,并且在视图上具有要创建的find
,则该用户还必须具有以下附加权限:
find
on the source collection or view.find
。find
on any other collections or views referenced in the pipeline
, if any.find
。A user with the 在数据库上具有readWrite
built in role on the database has the required privileges to run the listed operations. readWrite
内置角色的用户具有运行列出的操作所需的权限。Either create a user with the required role or grant the role to an existing user创建具有所需角色的用户或将该角色授予现有用户
Given a collection 提供了包含以下文件的集合survey
with the following documents:survey
:
The following operation creates a 以下操作使用managementFeedback
view with the _id
, feedback.management
, and department
fields:_id
、feedback.management
和department
字段创建managementFeedback
视图:
To query the view, you can use 要查询视图,可以在视图上使用db.collection.find()
on the view:db.collection.find()
:
The operation returns the following documents:该操作将返回以下文档:
The following operation performs an aggregation on the 以下操作在managementFeedback
view, using the $sortByCount
to group by the department
field and sort in descending order by the count of each distinct department:managementFeedback
视图上执行聚合,使用$sortByCount
以按department
字段分组并按每个不同部门的计数降序排序:
The operation returns the following documents:该操作将返回以下文档:
Given the following two collections:鉴于以下两个集合:
orders
collection:orders
集合:
inventory
collection:inventory
收集:
The following 以下db.createView()
example specifies a $lookup
stage to create a view from the join of the two collections:db.createView()
示例指定一个$lookup
阶段,用于从两个集合的联接创建视图:
To query the view, you can use 要查询视图,可以在视图上使用db.collection.find()
on the view:db.collection.find()
:
The operation returns the following documents:该操作将返回以下文档:
The following operation performs an aggregation on the 以下操作在orderDetails
view, using the $sortByCount
to group by the item
field and sort in descending order by the count of each distinct item:orderDetails
视图上执行聚合,使用$sortByCount
按项目字段分组,并按每个不同item
的计数降序排序:
The operation returns the following documents:该操作将返回以下文档:
Given the 给定带有以下文档的places
collection with the following document:places
集合:
The following operation creates a view, specifying collation at the view level:以下操作创建视图,并在视图级别指定排序规则:
String comparisons on the view use the view’s default collation. 视图上的字符串比较使用视图的默认排序规则。For example, the following operation uses the view’s collation:例如,以下操作使用视图的排序规则:
The operation returns 操作返回3
.3
。
An operation that attempts to change or override a view’s default collation will fail with an error.尝试更改或覆盖视图默认排序规则的操作将失败并出现错误。
See also参阅