Run projection query
Stay organized with collections
Save and categorize content based on your preferences.
Run a projection query.
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 content demonstrates how to run a projection query in Datastore mode across multiple programming languages including C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples focus on extracting specific properties, "priority" and "percent_complete", from entities in the Datastore.\u003c/p\u003e\n"],["\u003cp\u003eTo interact with Datastore, you'll need to utilize the respective Datastore mode client libraries for each language, and you can find instructions in the documentation.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication is required to access Datastore mode, and setting up Application Default Credentials is the suggested method for local development.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Cloud sample browser is recommended for finding code examples for other Google Cloud services.\u003c/p\u003e\n"]]],[],null,["# Run projection query\n\nRun a projection query.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Datastore queries](/datastore/docs/concepts/queries)\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 Query query = new Query(\"Task\")\n {\n Projection = { \"priority\", \"percent_complete\" }\n };\n List\u003clong\u003e priorities = new List\u003clong\u003e();\n List\u003cdouble\u003e percentCompletes = new List\u003cdouble\u003e();\n foreach (var entity in _db.RunQuery(query).Entities)\n {\n priorities.Add((long)entity[\"priority\"]);\n percentCompletes.Add((double)entity[\"percent_complete\"]);\n }\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 var priorities []int\n var percents []float64\n it := client.Run(ctx, query)\n for {\n \tvar task Task\n \tif _, err := it.Next(&task); err == iterator.Done {\n \t\tbreak\n \t} else if err != nil {\n \t\tlog.Fatal(err)\n \t}\n \tpriorities = append(priorities, task.Priority)\n \tpercents = append(percents, task.PercentComplete)\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 List\u003cLong\u003e priorities = new LinkedList\u003c\u003e();\n List\u003cDouble\u003e percentCompletes = new LinkedList\u003c\u003e();\n QueryResults\u003cProjectionEntity\u003e tasks = datastore.run(query);\n while (tasks.hasNext()) {\n ProjectionEntity task = tasks.next();\n priorities.add(task.getLong(\"priority\"));\n percentCompletes.add(task.getDouble(\"percent_complete\"));\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 async function runProjectionQuery() {\n const priorities = [];\n const percentCompletes = [];\n const [tasks] = await datastore.runQuery(query);\n tasks.forEach(task =\u003e {\n priorities.push(task.priority);\n percentCompletes.push(task.percent_complete);\n });\n\n return {\n priorities: priorities,\n percentCompletes: percentCompletes,\n };\n }\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 $priorities = array();\n $percentCompletes = array();\n $result = $datastore-\u003erunQuery($query);\n /* @var Entity $task */\n foreach ($result as $task) {\n $priorities[] = $task['priority'];\n $percentCompletes[] = $task['percent_complete'];\n }\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 priorities = []\n percent_completes = []\n\n for task in query.fetch():\n priorities.append(task[\"priority\"])\n percent_completes.append(task[\"percent_complete\"])\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 priorities = []\n percent_completes = []\n datastore.run(query).each do |task|\n priorities \u003c\u003c task[\"priority\"]\n percent_completes \u003c\u003c task[\"percent_complete\"]\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)."]]