View a markdown version of this page

$清单 SearchIndexes - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$清单 SearchIndexes

8.0.1 版中的新增内容。

Amazon DocumentDB 中的$listSearchIndexes聚合阶段返回有关集合中现有搜索索引的信息。它必须是聚合管道的第一阶段。

参数

  • name:(可选)要返回相关信息的搜索索引的名称。如果省略,则返回集合上的所有搜索索引。

输出字段

每个返回的文档都包含以下字段:

  • name:搜索索引的名称。

  • status:索引的构建状态。READY(已生成且有效)、BUILDING(正在进行并行构建)或FAILED(索引构建未成功完成)之一。

  • queryable:一个布尔值,表示该索引当前是否可用于提供查询。这false适用于隐藏的索引(仍然存在READY)和FAILED索引。隐藏索引是指的是 isb status u READY t queryable is false

  • latestDefinitionVersion:包含version(索引格式版本)和createdAt(索引创建时间)的文档。

语法

db.collection.aggregate([ { $listSearchIndexes: {} } ]) // Or filter by name: db.collection.aggregate([ { $listSearchIndexes: { name: "mySearchIndex" } } ])

示例(MongoDB Shell)

以下示例说明如何使用该$listSearchIndexes阶段列出集合上的所有搜索索引。

查询示例

db.movies.aggregate([ { $listSearchIndexes: {} } ]);

输出

[ { "name": "default", "status": "READY", "queryable": true, "latestDefinitionVersion": { "version": 2, "createdAt": ISODate("2026-05-11T21:12:57.974Z") } } ]

隐藏的索引仍然存在queryable: falseREADY但会报告,因为它在隐藏时不能用于提供查询:

[ { "name": "myHiddenIndex", "status": "READY", "queryable": false, "latestDefinitionVersion": { "version": 2, "createdAt": ISODate("2026-05-11T21:12:57.974Z") } } ]

要按特定索引名称进行筛选,请执行以下操作:

db.movies.aggregate([ { $listSearchIndexes: { name: "default" } } ]);

代码示例

要查看使用该$listSearchIndexes阶段的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const collection = db.collection('movies'); // List all search indexes const allIndexes = await collection.aggregate([ { $listSearchIndexes: {} } ]).toArray(); console.log('All search indexes:', allIndexes); // List a specific search index by name const namedIndex = await collection.aggregate([ { $listSearchIndexes: { name: "default" } } ]).toArray(); console.log('Named index:', namedIndex); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client['test'] collection = db['movies'] # List all search indexes all_indexes = list(collection.aggregate([ { '$listSearchIndexes': {} } ])) print('All search indexes:', all_indexes) # List a specific search index by name named_index = list(collection.aggregate([ { '$listSearchIndexes': { 'name': 'default' } } ])) print('Named index:', named_index) finally: client.close() example()