View a markdown version of this page

$listSearchIndexes - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

$listSearchIndexes

8.0.1 版的新功能。

Amazon DocumentDB $listSearchIndexes 中的彙總階段會傳回集合上現有搜尋索引的相關資訊。它必須是彙總管道中的第一個階段。

參數

  • name:(選用) 要傳回資訊的搜尋索引名稱。如果省略,則會傳回集合上的所有搜尋索引。

輸出欄位

每個傳回的文件都包含下列欄位:

  • name:搜尋索引的名稱。

  • status:索引的建置狀態。其中一個 READY(建置且有效)、 BUILDING(並行建置正在進行中) 或 FAILED(索引建置未成功完成)。

  • queryable:布林值,指出索引目前是否可以用於提供查詢。這是false針對隱藏的索引 (保持 READY) 和針對FAILED索引。隱藏索引是 statusREADYqueryable為 的情況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") } } ]

隱藏的索引會保留,READY但會報告 queryable: false,因為它無法在隱藏時用來提供查詢:

[ { "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()