Example datastore list tasks
Stay organized with collections
Save and categorize content based on your preferences.
Example datastore list tasks
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 document provides code samples in multiple languages (C#, Go, Java, Node.js, PHP, Python, Ruby) demonstrating how to list tasks from the Datastore.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample retrieves and lists tasks in ascending order based on their creation time.\u003c/p\u003e\n"],["\u003cp\u003eThe code snippets utilize the respective language's Datastore client library to interact with the Datastore service.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation guides the user on how to install the datastore library for different languages and how to access the API reference.\u003c/p\u003e\n"],["\u003cp\u003eSetting up Application Default Credentials is a step needed for authentication to the Datastore mode, further detail to do so can be found in the document.\u003c/p\u003e\n"]]],[],null,["Example datastore list tasks\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 /// Returns a list of all task entities in ascending order of creation time.\n /// \u003c/summary\u003e\n IEnumerable\u003cEntity\u003e ListTasks()\n {\n Query query = new Query(\"Task\")\n {\n Order = { { \"created\", PropertyOrder.Types.Direction.Descending } }\n };\n return _db.RunQuery(query).Entities;\n }\n\nGo\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 import (\n \t\"context\"\n \t\"log\"\n\n \t\"cloud.google.com/go/datastore\"\n )\n\n // ListTasks returns all the tasks in ascending order of creation time.\n func ListTasks(projectID string) ([]*Task, error) {\n \tctx := context.Background()\n \tclient, err := datastore.https://cloud.google.com/go/docs/reference/cloud.google.com/go/datastore/latest/index.html#cloud_google_com_go_datastore_Client_NewClient(ctx, projectID)\n \tif err != nil {\n \t\tlog.Fatalf(\"Could not create datastore client: %v\", err)\n \t}\n\n \tvar tasks []*Task\n \t// Create a query to fetch all Task entities, ordered by \"created\".\n \tquery := datastore.https://cloud.google.com/go/docs/reference/cloud.google.com/go/datastore/latest/index.html#cloud_google_com_go_datastore_Query_NewQuery(\"Task\").https://cloud.google.com/go/docs/reference/cloud.google.com/go/datastore/latest/index.html#cloud_google_com_go_datastore_Query_Order(\"created\")\n \tkeys, err := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/datastore/latest/index.html#cloud_google_com_go_datastore_Client_GetAll(ctx, query, &tasks)\n \tif err != nil {\n \t\treturn nil, err\n \t}\n\n \t// Set the id field on each Task from the corresponding key.\n \tfor i, key := range keys {\n \t\ttasks[i].id = key.ID\n \t}\n\n \treturn tasks, nil\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 * Returns a list of all task entities in ascending order of creation time.\n *\n * @throws DatastoreException if the query fails\n */\n Iterator\u003cEntity\u003e listTasks() {\n Query\u003cEntity\u003e query =\n Query.newEntityQueryBuilder().setKind(\"Task\").setOrderBy(OrderBy.asc(\"created\")).build();\n return datastore.run(query);\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 listTasks() {\n const query = datastore.createQuery('Task').order('created');\n\n const [tasks] = await datastore.runQuery(query);\n console.log('Tasks:');\n tasks.forEach(task =\u003e {\n const taskKey = task[datastore.KEY];\n console.log(taskKey.id, task);\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 * Return an iterator for all the tasks in ascending order of creation time.\n *\n * @param string $projectId The Google Cloud project ID.\n */\n function list_tasks(string $projectId)\n {\n $datastore = new DatastoreClient(['projectId' =\u003e $projectId]);\n\n $query = $datastore-\u003equery()\n -\u003ekind('Task')\n -\u003eorder('created');\n $result = $datastore-\u003erunQuery($query);\n /* @var Entity $task */\n foreach ($result as $index =\u003e $task) {\n printf('ID: %s' . PHP_EOL, $task-\u003ekey()-\u003epathEnd()['id']);\n printf(' Description: %s' . PHP_EOL, $task['description']);\n printf(' Status: %s' . PHP_EOL, $task['done'] ? 'done' : 'created');\n printf(' Created: %s' . PHP_EOL, $task['created']-\u003eformat('Y-m-d H:i:s e'));\n print(PHP_EOL);\n }\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 list_tasks(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 # Create a query against all of your objects of kind \"Task\"\n query = client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_query(kind=\"Task\")\n query.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.query.Query.html#google_cloud_datastore_query_Query_order = [\"created\"]\n\n return list(query.fetch())\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 list_tasks\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 query = datastore.query(\"Task\").order(\"created\")\n tasks = datastore.run query\n\n tasks.https://cloud.google.com/ruby/docs/reference/google-cloud-datastore/latest/Google-Cloud-Datastore-Properties.html do |t|\n puts t[\"description\"]\n puts t[\"done\"] ? \" Done\" : \" Not Done\"\n puts \" ID: #{t.key.id}\"\n end\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)."]]