以 Datastore 模式使用 Cloud Firestore
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Firestore 是 NoSQL 文件資料庫,專為自動調整資源配置、提供高效能,以及協助輕鬆開發應用程式而打造。這是 Datastore 的最新版本,推出了 Datastore 的多項改進功能。
由於 Datastore 模式中的 Firestore 是針對伺服器用途和 App Engine 進行最佳化,因此建議您將 Datastore 模式中的 Firestore 用於主要由 App Engine 應用程式使用的資料庫。原生模式的 Firestore 最適合用於行動裝置和即時通知用途。如要進一步瞭解 Firestore 模式,請參閱「選擇原生模式或 Datastore 模式」。
搭配 App Engine 使用 Datastore 模式
如何將 Datastore 模式與 App Engine 搭配使用:
如果您還未建立資料庫,請建立資料庫,並選擇 Datastore 模式的 Firestore。
您可以將現有的 Datastore 資料庫與 App Engine 應用程式搭配使用。這些現有的資料庫將自動升級為 Datastore 模式的 Firestore。
在應用程式中,將 google/cloud-datastore
加入應用程式的 composer.json
檔案,並從應用程式目錄執行 composer install
,藉以宣告 Datastore 為依附元件。進一步瞭解 composer。
使用 Google Cloud 用戶端程式庫讀取及寫入實體,以及查詢資料。
設定索引
Datastore 模式會對應用程式執行的每個查詢使用索引。這些索引會隨著實體的變更一併更新,因此應用程式執行查詢時,能夠快速得到結果。
Datastore 模式會自動建立適用於簡單查詢類型的單一屬性索引。如要執行包含多個屬性的複雜查詢,您必須在應用程式的 index.yaml
檔案中設定複合式索引。
App Engine 開發伺服器將會更新您的 index.yaml
檔案,使其包含執行測試所需的複合式索引。同樣地,在您執行測試時,Datastore 模式模擬器也會產生索引。
如果您未執行本機測試或您的測試並未包含複雜查詢,您也可以手動將索引新增至應用程式的 index.yaml
檔案。
設定資料庫權限
根據預設,您的應用程式具有讀取及寫入您Google Cloud 專案中 Datastore 模式和 Firestore 資料庫的所有必要權限。
為了管理這些權限,每個 App Engine 應用程式都會使用預設服務帳戶,該帳戶具有與應用程式相同專案中 Datastore 模式和 Firestore 資料庫的完整讀寫權限。您可以變更預設服務帳戶的權限,但除非您指派具備必要權限的 IAM 角色,否則應用程式可能會失去存取權。
要瞭解如何讓其他使用者、應用程式或專案存取資料庫,請參閱存取資料庫一文。
使用 Datastore 模式模擬器進行本機測試
Google Cloud CLI 包含實際工作環境 Datastore 模式環境的本機模擬器。您可以使用模擬器在本機上開發及測試應用程式。此外,模擬器還可幫您對實際運作的 Datastore 模式執行個體產生索引,以及刪除不需要的索引。
如果您使用 App Engine 本機開發伺服器測試應用程式,可在啟動伺服器時設定 --support_datastore_emulator=true
標記,確保伺服器會使用 Datastore 模式模擬器。
如果您正在使用 Datastore 模式模擬器,dev_appserver 將會顯示如下訊息:
... Using Cloud Datastore Emulator.
定價、配額與限制
Datastore 模式會提供免費配額,但設有每日上限。付費帳戶享有無限的儲存空間,以及不限次數的讀取和寫入作業。詳情請參閱 Datastore 配額頁面。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-06-16 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-06-16 (世界標準時間)。"],[[["Firestore, an upgraded version of Datastore, is a NoSQL document database designed for automatic scaling, high performance, and straightforward application development, offering both Native mode and Datastore mode."],["Firestore in Datastore mode is recommended for App Engine applications, while Native mode is better suited for mobile and real-time notification use cases, with existing Datastore databases automatically upgrading to this mode."],["Datastore mode relies on indexes for all application queries, with automatic creation for single-property indexes and manual or emulator-assisted creation for complex composite indexes in the `index.yaml` file."],["App Engine apps have default service account permissions for Datastore mode and Firestore databases within the same Google Cloud project, though permissions can be adjusted via IAM roles."],["The Datastore mode emulator, part of the Google Cloud CLI, allows for local development and testing, including index generation and deletion, and can be used in conjunction with the App Engine local development server."]]],[]]