Go 1.11 已达到支持终止期限,将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Go 1.11 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Go 1.11 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Go 版本。
使用 Datastore 模式 Cloud Firestore
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Firestore 是一个 NoSQL 文档数据库,能够自动扩缩、具备出色的性能,并且易于进行应用开发。Firestore 是 Datastore 的最新版本;与 Datastore 相比,这个新版本引入了多项改进功能。
由于 Datastore 模式 Firestore 针对服务器用例和 App Engine 进行了优化,因此,对于主要将由 App Engine 应用使用的数据库,我们建议使用 Datastore 模式 Firestore。原生模式 Firestore 最适合移动和实时通知用例。如需详细了解 Firestore 模式,请参阅选择原生模式或 Datastore 模式。
将 Datastore 模式与 App Engine 结合使用
如需将 Datastore 模式与 App Engine 搭配使用,请执行以下操作:
如果您尚未这样做,请创建数据库并选择“Datastore 模式 Firestore”。
您可以将现有的 Datastore 数据库与 App Engine 应用搭配使用。这些现有数据库将自动升级为 Datastore 模式 Firestore。
在您的应用中,使用 go get cloud.google.com/go/datastore
命令将 Datastore 声明为依赖项。或者,您也可以在应用的 go.mod
文件中声明依赖项。请详细了解如何指定依赖项。
使用 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 配额页面。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-20。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[[["\u003cp\u003eFirestore, an upgraded version of Datastore, is a NoSQL document database designed for automatic scaling, high performance, and ease of application development.\u003c/p\u003e\n"],["\u003cp\u003eFirestore in Datastore mode is recommended for server-centric applications, particularly those on App Engine, while Native mode suits mobile and real-time notification use cases.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine apps can leverage Datastore mode by declaring it as a dependency and using the Google Cloud Client Library for entity and data management.\u003c/p\u003e\n"],["\u003cp\u003eDatastore mode uses indexes for all queries, automatically creating single-property indexes and requiring the manual configuration of composite indexes in the \u003ccode\u003eindex.yaml\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eThe Datastore mode emulator, included with the Google Cloud CLI, facilitates local testing and development and can aid in generating necessary indexes.\u003c/p\u003e\n"]]],[],null,["# Using Cloud Firestore in Datastore Mode\n\nFirestore is a NoSQL document database built for automatic scaling,\nhigh performance, and ease of application development. It is the newest version\nof Datastore and introduces several improvements over\nDatastore.\n\nBecause Firestore in [Datastore mode](/datastore/docs)\nis optimized for server use cases and for\nApp Engine, we recommend using Firestore in\nDatastore mode for databases that will be used primarily by\nApp Engine apps. Firestore in Native mode is most useful for\nmobile and real-time notification use cases. For more information about\nFirestore modes, see [Choosing between Native Mode and\nDatastore mode](/datastore/docs/firestore-or-datastore).\n\nUsing Datastore mode with App Engine\n------------------------------------\n\nTo use Datastore mode with App Engine:\n\n1. If you haven't already done so,\n [create a database](/datastore/docs/store-query-data#create_a_database)\n and choose Firestore in Datastore mode.\n\n You can use existing Datastore databases with App Engine\n apps. These existing databases [will be automatically upgraded to\n Firestore in Datastore\n mode](/datastore/docs/upgrade-to-firestore).\n\n\n 1. In your app, declare Datastore as a dependency\n\n by using the `go get cloud.google.com/go/datastore` command. Alternatively,\n you can declare dependencies in your app's `go.mod` file. Learn more\n [about specifying dependencies](/appengine/docs/legacy/standard/go111/specifying-dependencies).\n\n \u003cbr /\u003e\n\n 2. Use the Google Cloud Client Library to\n [read and write entities](/datastore/docs/concepts/entities)\n and to [query data](/datastore/docs/concepts/queries).\n\n Configuring indexes\n -------------------\n\n Datastore mode uses\n [indexes](/datastore/docs/concepts/indexes)\n for every query your application makes. The indexes are updated whenever an\n entity changes, so the results can be returned quickly when the app makes a\n query.\n\n Datastore mode automatically creates single-property indexes for use with\n simple types of queries. For complex queries that include multiple properties,\n you'll need to configure composite indexes in your app's `index.yaml` file.\n\n The App Engine development server will update your `index.yaml` file\n with the composite indexes needed to run your tests. Similarly, the\n [Datastore mode emulator can generate indexes](/datastore/docs/tools)\n when you run tests.\n\n You can also add the indexes to your app's\n `index.yaml` file [manually](/appengine/docs/legacy/standard/go111/configuring-datastore-indexes-with-index-yaml) if you do not run local\n tests or your tests do not include complex queries.\n\n Setting database permissions\n ----------------------------\n\n By default, your app has all the permissions required to read and write to\n Datastore mode and Firestore databases in your\n Google Cloud project.\n\n To manage these permissions, each App Engine app uses a [default service\n account](/appengine/docs/legacy/standard/go111/service-account)\n that gives full read and write access to Datastore mode\n and Firestore databases in the same project as the app. You can\n [change the permissions of the default service\n account](/appengine/docs/legacy/standard/go111/access-control#modify-service-account),\n but your app may lose access unless you assign an IAM role with the\n [required permissions](/datastore/docs/access/iam#required_permissions).\n\n For information about allowing other users, apps, or projects to access a database, see\n [Accessing your database](/datastore/docs/activate).\n\n Using the Datastore mode emulator for local testing\n ---------------------------------------------------\n\n The Google Cloud CLI includes a local [emulator of the production Datastore mode\n mode environment](/datastore/docs/tools/datastore-emulator). You can use the emulator to develop and test your application locally. In addition, the emulator can help you generate indexes for your production Datastore mode mode instance and delete unneeded indexes.\n\n \u003cbr /\u003e\n\n If you use the App Engine [local development\n server](/appengine/docs/legacy/standard/python/tools/using-local-server) to test your\n app, you can ensure that the server will use the Datastore mode\n emulator by setting the `--support_datastore_emulator=true` flag when starting\n the server.\n\n If you are using the Datastore mode emulator, dev_appserver will display: \n\n ... Using Cloud Datastore Emulator.\n\n Pricing, quotas, and limits\n ---------------------------\n\n Datastore mode offers a free quota with daily limits. Paid\n accounts offer unlimited storage, read, and write operations. More information\n is available on the [Datastore Quotas](/appengine/docs/legacy/standard/go111/quotas#Datastore) page."]]