对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

listIndexes(数据库命令)

listIndexes

返回指定集合上的索引信息,包括隐藏索引和当前正在构建的索引。返回的索引信息包括用于创建索引的键和选项。 您可以选择设立第批处理结果的批处理大小。

提示

In mongosh, this command can also be run through the db.collection.getIndexes() helper method.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

该命令具有以下语法:

db.runCommand (
{
listIndexes: "<collection-name>",
cursor: { batchSize: <int> },
comment: <any>
}
)

该命令接受以下字段:

字段
类型
说明

listIndexes

字符串

集合的名称。

cursor.batchSize

整型

可选。指定游标批处理大小。

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

listIndexes 命令上设置的任何注释都将由在 listIndexes 游标上运行的任何后续 getMore 命令继承。

If access control is enforced, the built-in read role provides the required privileges to run listIndexes for the collections in a database.

listIndexes does not return information on Atlas Search indexes. Instead, use $listSearchIndexes.

If the client that issued listIndexes disconnects before the operation completes, MongoDB marks listIndexes for termination using killOp.

To run on a replica set member, listIndexes operations require the member to be in PRIMARY or SECONDARY state. If the member is in another state, such as STARTUP2, the operation errors.

从 MongoDB 6.3、6.0.5 和 5.0.16 开始,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.cursor

中游标指定的批处理大小返回结果集。批处理输出中的每个文档都包含以下字段:

字段
类型
说明

id

整型

:64位整型。如果为零,则不再有批量信息。如果非零,则为游标 ID,可在 getMore 命令中使用以获取下一批索引信息。

ns

字符串

数据库和集合名称采用以下格式: <database-name>.<collection-name>

firstBatch

文档

索引信息包括用于创建索引的键和选项。仅当值为 true 时,隐藏的索引选项才会显示。

根据需要使用 getMore 检索其他结果。

listIndexes.ok

命令的返回值。值为 1 表示成功。

此示例列出 contacts 集合的索引,但未指定游标批处理大小。

1db.runCommand (
2 {
3 listIndexes: "contacts"
4 }
5)
1{
2 cursor: {
3 id: Long("0"),
4 ns: 'test.contacts',
5 firstBatch: [
6 { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' },
7 { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' }
8 ]
9 },
10 ok: 1
11}

此示例列出 contacts 集合的索引,并指定游标批处理大小为 1。

1db.runCommand (
2 {
3 listIndexes: "contacts", cursor: { batchSize: 1 }
4 }
5)
1{
2 cursor: {
3 id: Long("4809221676960028307"),
4 ns: 'test.contacts',
5 firstBatch: [ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' } ]
6 },
7 ok: 1
8}

此示例使用 getMorecontacts 集合中检索更多结果批次。

1db.runCommand(
2 {
3 getMore: Long("4809221676960028307"), collection: "contacts"
4 }
5)
1{
2 cursor: {
3 nextBatch: [ { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' } ],
4 id: Long("0"),
5 ns: 'test.contacts'
6 },
7 ok: 1
8}