更新
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
更新实体。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides code samples and information on how to update an entity in Datastore mode.\u003c/p\u003e\n"],["\u003cp\u003eThe content includes examples in C#, Go, Java, Node.js, PHP, Python, and Ruby, showcasing various approaches to updating an entity.\u003c/p\u003e\n"],["\u003cp\u003eEach language section offers links to the respective Datastore mode client libraries, API reference documentation, and authentication setup instructions.\u003c/p\u003e\n"],["\u003cp\u003eUpdating an entity involves retrieving the entity, modifying its properties, and then persisting the updated entity back to Datastore.\u003c/p\u003e\n"],["\u003cp\u003eTransactions are used to ensure data consistency when making changes to entities in Datastore, as illustrated in the code samples for multiple programming languages.\u003c/p\u003e\n"]]],[],null,["# Update an entity.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Entities, Properties, and Keys](/datastore/docs/concepts/entities)\n\nCode sample\n-----------\n\n### C#\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode C# API\nreference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n _sampleTask[\"priority\"] = 5;\n _db.Update(_sampleTask);\n\n### Go\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Go API\nreference documentation](https://cloud.google.com/go/docs/reference/cloud.google.com/go/datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n taskKey := datastore.NameKey(\"Task\", \"sampleTask\", nil)\n tx, err := client.NewTransaction(ctx)\n if err != nil {\n \tlog.Fatalf(\"client.NewTransaction: %v\", err)\n }\n var task Task\n if err := tx.Get(taskKey, &task); err != nil {\n \tlog.Fatalf(\"tx.Get: %v\", err)\n }\n task.Priority = 5\n if _, err := tx.Put(taskKey, &task); err != nil {\n \tlog.Fatalf(\"tx.Put: %v\", err)\n }\n if _, err := tx.Commit(); err != nil {\n \tlog.Fatalf(\"tx.Commit: %v\", err)\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-datastore/latest/history).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n Entity task;\n Transaction txn = datastore.newTransaction();\n try {\n task = Entity.newBuilder(txn.get(taskKey)).set(\"priority\", 5).build();\n txn.put(task);\n txn.commit();\n } finally {\n if (txn.isActive()) {\n txn.rollback();\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Node.js API\nreference documentation](https://cloud.google.com/nodejs/docs/reference/datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const taskKey = datastore.key('Task');\n const task = {\n category: 'Personal',\n done: false,\n priority: 4,\n description: 'Learn Cloud Datastore',\n };\n\n const entity = {\n key: taskKey,\n data: task,\n };\n\n await datastore.update(entity);\n // Task updated successfully.\n\n### PHP\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode PHP API\nreference documentation](https://googleapis.github.io/google-cloud-php/#/docs/cloud-datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n $transaction = $datastore-\u003etransaction();\n $key = $datastore-\u003ekey('Task', 'sampleTask');\n $task = $transaction-\u003elookup($key);\n $task['priority'] = 5;\n $transaction-\u003eupdate($task);\n $transaction-\u003ecommit();\n\n### Python\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Python API\nreference documentation](https://cloud.google.com/python/docs/reference/datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/datastore/latest/\n\n # For help authenticating your client, visit\n # https://cloud.google.com/docs/authentication/getting-started\n client = https://cloud.google.com/python/docs/reference/datastore/latest/.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html()\n\n with client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_transaction():\n key = client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_key(\"Task\", \"sampleTask\")\n task = client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_get(key)\n\n task[\"done\"] = True\n\n client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_put(task)\n\n### Ruby\n\n\nTo learn how to install and use the client library for Datastore mode, see\n[Datastore mode client libraries](/datastore/docs/reference/libraries).\n\n\nFor more information, see the\n[Datastore mode Ruby API\nreference documentation](/ruby/docs/reference/google-cloud-datastore/latest).\n\n\nTo authenticate to Datastore mode, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # task_name = \"sampleTask\"\n datastore.transaction do |_tx|\n task = datastore.find \"Task\", task_name\n task[\"priority\"] = 5\n datastore.save task\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=datastore)."]]