| 僅適用於 Cloud Firestore Enterprise 版。 | 
本頁說明如何管理索引。如要進一步瞭解索引,請參閱索引總覽。
事前準備
如要在與 MongoDB 相容的 Cloud Firestore 中建立索引,請務必先獲派下列任一角色:
- roles/datastore.owner
- roles/datastore.indexAdmin
- roles/editor
- roles/owner
如要授予角色,請參閱授予單一角色。 如要進一步瞭解 Cloud Firestore 角色和相關聯的權限,請參閱「預先定義角色」。
如果您已定義自訂角色,請指派下列所有權限來建立索引:
- datastore.indexes.create
- datastore.indexes.delete
- datastore.indexes.get
- datastore.indexes.list
- datastore.indexes.update
建立索引
如要建立索引,請完成下列步驟:
MongoDB API
使用 createIndex() 方法建立索引。例如:
- 
  db.restaurants.createIndex({"cuisine" : 1}) 
- 
  db.restaurants.createIndex({"cuisine" : 1}, {sparse: true}) 
- 
  系統也支援使用 db.runCommand()建立索引,但最多只能建立一個索引。db.runCommand({"createIndexes":"restaurant", "index": [{"key": {"cuisine":1}, {"name": "cuisine_index"}]}) 
請注意下列限制:
Firebase 控制台
gcloud CLI
如要建立索引,請使用 gcloud firestore indexes composite create 指令。將 api-scope 設為 mongodb-compatible-api。
gcloud firestore indexes composite create \ --database='DATABASE_ID' \ --collection-group=COLLECTION \ --field-config=FIELD_CONFIGURATION \ --query-scope=collection-group \ --density=dense \ --api-scope=mongodb-compatible-api
更改下列內容:
- DATABASE_ID:資料庫 ID。
- COLLECTION:集合名稱。
- FIELD_CONFIGURATION:欄位設定。針對每個欄位,
    新增 --field-config=field-path=。例如:--field-config=field-path=user-id,order=descending \ --field-config=field-path=score,order=descending如要進一步瞭解如何設定這些欄位,請參閱 --field-config。
如要建立稀疏索引,請設定 --density=sparse-any。
如要建立多鍵索引,請新增 --multikey 標記。
如要建立不重複的索引,請新增 --unique 旗標。
Terraform
使用 google_firestore_index 資源,並將 api_scope 設為 MONGODB_COMPATIBLE_API,以及將 query_scope 設為 COLLECTION_GROUP。
resource "google_firestore_index" "index" { database = "DATABASE_ID" collection = "COLLECTION" api_scope = "MONGODB_COMPATIBLE_API" query_scope = "COLLECTION_GROUP" // You can include multiple field blocks fields { field_path = "FIELD_PATH" order = "ORDER" } // Optional multikey = true density = "DENSITY" }
更改下列內容:
- DATABASE_ID:所選資料庫的資料庫 ID
- COLLECTION:要建立索引的集合名稱
- FIELD_PATH:要建立索引的欄位名稱
- ORDER:ASCENDING或DESCENDING
- DENSITY:SPARSE_ANY或DENSE
刪除索引
如要刪除索引,請完成下列步驟:
MongoDB API
使用 dropIndex() 方法刪除索引。例如:
使用索引名稱刪除索引
db.restaurants.dropIndex("cuisine_index")
使用索引定義刪除索引
db.restaurants.dropIndex({"cuisine" : 1})
Firebase 控制台
- 
前往 Firebase 控制台的「Firestore Database」頁面。 
- 從資料庫清單中選取資料庫。
- 按一下「索引」分頁標籤。
- 在索引清單中,針對要刪除的索引,從「更多」按鈕 選擇「刪除」。
- 按一下「刪除索引」。
gcloud CLI
- 如要尋找索引名稱,請使用 - gcloud firestore indexes composite list指令。- gcloud firestore indexes composite list \ --database='DATABASE_ID' - 將 DATABASE_ID 替換為資料庫 ID。 
- 
如要刪除索引,請使用 gcloud firestore indexes composite delete指令。gcloud firestore indexes composite delete INDEX_NAME \ --database='DATABASE_ID' 更改下列內容: - INDEX_NAME:索引名稱
- DATABASE_ID:資料庫 ID
 
索引建構時間
如要建構索引,與 MongoDB 相容的 Cloud Firestore 必須先建立索引,然後使用現有資料回填索引項目。建立索引所需的時間取決於下列因素:
- 即使是空白資料庫,建立索引也至少需要幾分鐘。 
- 補充索引項目所需的時間,取決於新索引中現有資料的數量。與索引定義相符的欄位值越多,回填索引項目的時間就越長。 
管理長時間執行的作業
建立索引是長時間執行的作業。以下各節說明如何處理索引的長時間執行作業。
開始建立索引後,與 MongoDB 相容的 Cloud Firestore 會為作業指派專屬名稱。作業名稱的前置字串為 projects/PROJECT_ID/databases/DATABASE_ID/operations/,例如:
projects/PROJECT_ID/databases/DATABASE_ID/operations/ASA1MTAwNDQxNAgadGx1YWZlZAcSeWx0aGdpbi1zYm9qLW5pbWRhEgopEg
為 describe 指令指定作業名稱時,可以省略前置字串。
列出所有長時間執行的作業
如要列出長時間執行的作業,請使用 gcloud firestore operations list 指令。這個指令會列出進行中和最近完成的作業。
匯出完成後其項目會保留幾天以供存取:
gcloud firestore operations list
檢查作業狀態
您可以列出單一作業的詳細資料,而不必列出所有長時間執行的作業:
gcloud firestore operations describe operation-name
估算完成時間
作業執行時,可查看 state 欄位值,以瞭解作業的整體狀態。
用於取得長時間執行作業狀態的要求,也會傳回 workEstimated 和workCompleted 指標。workEstimated 顯示作業預計處理的文件總數。workCompleted
會顯示目前處理的文件數量。作業完成後,workCompleted 會反映出實際處理的文件總數,可能與 workEstimated 的值不同。
如要估算作業進度,請將 workCompleted 除以 workEstimated。
以下是建立索引的進度範例:
{
  "operations": [
    {
      "name": "projects/project-id/operations/AyAyMDBiM2U5NTgwZDAtZGIyYi0zYjc0LTIzYWEtZjg1ZGdWFmZWQHEjF0c2Flc3UtcmV4ZWRuaS1uaW1kYRUKSBI",
      "metadata": {
        "@type": "type.googleapis.com/google.firestore.admin.v1.IndexOperationMetadata",
        "common": {
          "operationType": "CREATE_INDEX",
          "startTime": "2020-06-23T16:52:25.697539Z",
          "state": "PROCESSING"
        },
        "progressDocuments": {
          "workCompleted": "219327",
          "workEstimated": "2198182"
        }
       },
    },
    ...
作業完成時,作業說明中會包含 "done": true。查看 state 欄位值可得知作業結果。如果回應中未設定 done 欄位,則表示作業尚未完成。