Add task
Stay organized with collections
Save and categorize content based on your preferences.
Creates and saves an Entity of the hypothetical type "Task"
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates how to create and save a "Task" entity in Datastore using various programming languages.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples show how to define the "Task" entity with fields like "description," "created," and "done" in C#, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe examples illustrate the process of setting up the Datastore client and creating or allocating a key for the new entity before its creation.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples use the 'datastore.save', 'datastore.put' or 'datastore.insert' commands to save the new entity.\u003c/p\u003e\n"],["\u003cp\u003eThe page provides links to the Datastore client libraries and API reference documentation for each language and also authentication documentation.\u003c/p\u003e\n"]]],[],null,["Creates and saves an Entity of the hypothetical type \"Task\"\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Getting started with the Firestore in Datastore mode API](/datastore/docs/datastore-api-tutorial)\n\nCode sample \n\nC#\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 /// \u003csummary\u003e\n /// Adds a task entity to the Datastore\n /// \u003c/summary\u003e\n /// \u003cparam name=\"description\"\u003eThe task description.\u003c/param\u003e\n /// \u003creturns\u003eThe key of the entity.\u003c/returns\u003e\n Key AddTask(string description)\n {\n Entity task = new Entity()\n {\n Key = _keyFactory.CreateIncompleteKey(),\n [\"description\"] = new Value()\n {\n StringValue = description,\n ExcludeFromIndexes = true\n },\n [\"created\"] = DateTime.UtcNow,\n [\"done\"] = false\n };\n return _db.Insert(task);\n }\n\nJava\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 /**\n * Adds a task entity to the Datastore.\n *\n * @param description The task description\n * @return The {@link Key} of the entity\n * @throws DatastoreException if the ID allocation or put fails\n */\n Key addTask(String description) {\n Key key = datastore.allocateId(keyFactory.newKey());\n Entity task =\n Entity.newBuilder(key)\n .set(\n \"description\",\n StringValue.newBuilder(description).setExcludeFromIndexes(true).build())\n .set(\"created\", Timestamp.now())\n .set(\"done\", false)\n .build();\n datastore.put(task);\n return key;\n }\n\nNode.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 async function addTask(description) {\n const taskKey = datastore.key('Task');\n const entity = {\n key: taskKey,\n data: [\n {\n name: 'created',\n value: new Date().toJSON(),\n },\n {\n name: 'description',\n value: description,\n excludeFromIndexes: true,\n },\n {\n name: 'done',\n value: false,\n },\n ],\n };\n\n try {\n await datastore.save(entity);\n console.log(`Task ${taskKey.id} created successfully.`);\n } catch (err) {\n console.error('ERROR:', err);\n }\n }\n\nPHP\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 use Google\\Cloud\\Datastore\\DatastoreClient;\n\n /**\n * Create a new task with a given description.\n *\n * @param string $projectId The Google Cloud project ID.\n * @param string $description\n */\n function add_task(string $projectId, string $description)\n {\n $datastore = new DatastoreClient(['projectId' =\u003e $projectId]);\n\n $taskKey = $datastore-\u003ekey('Task');\n $task = $datastore-\u003eentity(\n $taskKey,\n [\n 'created' =\u003e new DateTime(),\n 'description' =\u003e $description,\n 'done' =\u003e false\n ],\n ['excludeFromIndexes' =\u003e ['description']]\n );\n $datastore-\u003einsert($task);\n printf('Created new task with ID %d.' . PHP_EOL, $task-\u003ekey()-\u003epathEnd()['id']);\n }\n\nPython\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 def add_task(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, description: str):\n # Create an incomplete key for an entity of kind \"Task\". An incomplete\n # key is one where Datastore will automatically generate an Id\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\")\n\n # Create an unsaved Entity object, and tell Datastore not to index the\n # `description` field\n task = https://cloud.google.com/python/docs/reference/datastore/latest/.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.entity.Entity.html(key, exclude_from_indexes=(\"description\",))\n\n # Apply new field values and save the Task entity to Datastore\n task.update(\n {\n \"created\": datetime.datetime.now(tz=datetime.timezone.utc),\n \"description\": description,\n \"done\": False,\n }\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 return task.key\n\nRuby\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 def add_task description\n require \"google/cloud/datastore\"\n\n datastore = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-datastore/latest/Google-Cloud-Datastore.html.https://cloud.google.com/ruby/docs/reference/google-cloud-datastore/latest/Google-Cloud-Datastore.html\n\n task = datastore.entity \"Task\" do |t|\n t[\"description\"] = description\n t[\"created\"] = Time.now\n t[\"done\"] = false\n t.https://cloud.google.com/ruby/docs/reference/google-cloud-datastore/latest/Google-Cloud-Datastore-Entity.html \"description\", true\n end\n\n datastore.save task\n\n puts task.key.id\n\n task.key.id\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=datastore)."]]