db.collection.getIndexes()
On this page本页内容
Definition定义
db.collection.getIndexes()
- Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for database commands or language-specific drivers, such as Node.js.For the database command, see the
listIndexes
command.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Returns an array that holds a list of documents that identify and describe the existing indexes on the collection, including hidden indexes.返回一个数组,该数组包含用于标识和描述集合上现有索引(包括隐藏索引)的文档列表。You must call必须对集合调用db.collection.getIndexes()
on a collection.db.collection.getIndexes()
。For example:例如:db.collection.getIndexes()
Change将collection
to the name of the collection for which to return index information.collection
更改为要返回其索引信息的集合的名称。
Behavior行为
Client Disconnection客户端断开连接
Starting in MongoDB 4.2, if the client that issued 从MongoDB 4.2开始,如果发出db.collection.getIndexes()
disconnects before the operation completes, MongoDB marks db.collection.getIndexes()
for termination using killOp
.db.collection.getIndexes()
的客户端在操作完成前断开连接,MongoDB会使用killOp
标记db.collection.getIndexes()
终止。
Replica Set Member State Restriction副本集成员状态限制
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,listIndexes
operations require the member to be in PRIMARY
or SECONDARY
state. listIndexes
操作需要该成员处于PRIMARY
或SECONDARY
状态。If the member is in another state, such as 如果成员处于其他状态,例如STARTUP2
, the operation errors.STARTUP2
,则操作将出错。
In previous versions, the operations also run when the member is in 在以前的版本中,成员处于STARTUP2
. The operations wait until the member transitioned to RECOVERING
.STARTUP2
时也会运行这些操作。操作将等待,直到成员转换到RECOVERING
。
Wildcard Indexes通配符索引
Starting in MongoDB 6.3, 6.0.5, and 5.0.16, the 从MongoDB 6.3、6.0.5和5.0.16开始,wildcardProjection
field stores the index projection in its submitted form. Earlier versions of the server may have stored the projection in a normalized form.wildcardProjection
字段以提交的形式存储索引投影。早期版本的服务器可能以规范化的形式存储了投影。
The server uses the index the same way, but you may notice a difference in the output of the 服务器以相同的方式使用索引,但您可能会注意到listIndexes
and db.collection.getIndexes()
commands.listIndexes
和db.collection.getIndexes()
命令的输出有所不同。
Atlas Search IndexesAtlas搜索索引
getIndexes()
does not return information on Atlas Search indexes.不会返回有关Atlas Search索引的信息。
Required Access所需访问权限
To run 若要在强制执行访问控制时运行db.collection.getIndexes()
when access control is enforced, users must have privileges to listIndexes
on the collection.db.collection.getIndexes()
,用户必须具有在集合上listIndexes
的权限。
The built-in role 内置的角色read
provides the required privileges to run db.collection.getIndexes()
for the collections in a database.read
提供了为数据库中的集合运行db.collection.getIndexes()
所需的权限。
Output输出
db.collection.getIndexes()
returns an array of documents that hold index information for the collection. 返回一个文档数组,其中包含集合的索引信息。For example:例如:
Starting in MongoDB 4.4, 从MongoDB 4.4开始,db.collection.getIndexes()
no longer includes the ns
field.db.collection.getIndexes()
不再包含ns
字段。
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"status" : 1
},
"name" : "status_1"
},
{
"v" : 2,
"key" : {
"points" : 1
},
"name" : "points_1"
}
]
Index information includes the keys and options used to create the index. The index option 索引信息包括用于创建索引的键和选项。索引选项hidden
, available starting in MongoDB 4.4, is only available if the value is true
.hidden
从MongoDB 4.4开始可用,只有当值为true
时才可用。
For information on the keys and index options, see 有关键和索引选项的信息,请参阅db.collection.createIndex()
.db.collection.createIndex()
。