Property by kind query
Stay organized with collections
Save and categorize content based on your preferences.
Query property by kind.
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 query property data by kind in Google Cloud Datastore mode across multiple programming languages like C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eEach code example showcases querying the \u003ccode\u003e__property__\u003c/code\u003e kind with a filter to identify properties that have a specific ancestor, such as a "Task" within the \u003ccode\u003e__kind__\u003c/code\u003e kind.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples also outline how to access and process the 'property_representation' within entities, extracting the representations of properties.\u003c/p\u003e\n"],["\u003cp\u003eThe content refers the reader to the relevant documentation for each language such as the client libraries and API references, as well as how to authenticate to the Datastore.\u003c/p\u003e\n"],["\u003cp\u003eThe provided examples will guide users through using the Google Cloud sample browser to search and filter code samples for other products.\u003c/p\u003e\n"]]],[],null,["# Property by kind query\n\nQuery property by kind.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Datastore Metadata](/datastore/docs/concepts/metadataqueries)\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 Key key = _db.CreateKeyFactory(\"__kind__\").CreateKey(\"Task\");\n Query query = new Query(\"__property__\")\n {\n Filter = Filter.HasAncestor(key)\n };\n var properties = new List\u003cstring\u003e();\n foreach (Entity entity in _db.RunQuery(query).Entities)\n {\n string kind = entity.Key.Path[0].Name;\n string property = entity.Key.Path[1].Name;\n var representations = entity[\"property_representation\"]\n .ArrayValue.Values.Select(x =\u003e x.StringValue)\n .OrderBy(x =\u003e x);\n properties.Add($\"{property}:\" +\n string.Join(\",\", representations));\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 kindKey := datastore.NameKey(\"__kind__\", \"Task\", nil)\n query := datastore.NewQuery(\"__property__\").Ancestor(kindKey)\n\n type Prop struct {\n \tRepr []string `datastore:\"property_representation\"`\n }\n\n var props []Prop\n keys, err := client.GetAll(ctx, query, &props)\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 Key key = datastore.newKeyFactory().setKind(\"__kind__\").newKey(\"Task\");\n Query\u003cEntity\u003e query =\n Query.newEntityQueryBuilder()\n .setKind(\"__property__\")\n .setFilter(PropertyFilter.hasAncestor(key))\n .build();\n QueryResults\u003cEntity\u003e results = datastore.run(query);\n Map\u003cString, Collection\u003cString\u003e\u003e representationsByProperty = new HashMap\u003c\u003e();\n while (results.hasNext()) {\n Entity result = results.next();\n String propertyName = result.getKey().getName();\n List\u003cStringValue\u003e representations = result.getList(\"property_representation\");\n Collection\u003cString\u003e currentRepresentations = representationsByProperty.get(propertyName);\n if (currentRepresentations == null) {\n currentRepresentations = new HashSet\u003c\u003e();\n representationsByProperty.put(propertyName, currentRepresentations);\n }\n for (StringValue value : representations) {\n currentRepresentations.add(value.get());\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 async function runPropertyByKindQuery() {\n const ancestorKey = datastore.key(['__kind__', 'Account']);\n\n const query = datastore\n .createQuery('__property__')\n .hasAncestor(ancestorKey);\n const [entities] = await datastore.runQuery(query);\n\n const representationsByProperty = {};\n\n entities.forEach(entity =\u003e {\n const key = entity[datastore.KEY];\n const propertyName = key.name;\n const propertyType = entity.property_representation;\n\n representationsByProperty[propertyName] = propertyType;\n });\n\n console.log('Task property representations:');\n for (const key in representationsByProperty) {\n console.log(key, representationsByProperty[key]);\n }\n\n return representationsByProperty;\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 $ancestorKey = $datastore-\u003ekey('__kind__', 'Task');\n $query = $datastore-\u003equery()\n -\u003ekind('__property__')\n -\u003ehasAncestor($ancestorKey);\n $result = $datastore-\u003erunQuery($query);\n /* @var array\u003cstring,string\u003e $properties */\n $properties = [];\n /* @var Entity $entity */\n foreach ($result as $entity) {\n $propertyName = $entity-\u003ekey()-\u003epath()[1]['name'];\n $propertyType = $entity['property_representation'];\n $properties[$propertyName] = $propertyType;\n }\n // Example values of $properties: ['description' =\u003e ['STRING']]\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 ancestor = client.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.client.Client.html#google_cloud_datastore_client_Client_key(\"__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=\"__property__\", ancestor=ancestor)\n\n representations_by_property = {}\n\n for entity in query.fetch():\n property_name = entity.key.https://cloud.google.com/python/docs/reference/datastore/latest/google.cloud.datastore.key.Key.html#google_cloud_datastore_key_Key_name\n property_types = entity[\"property_representation\"]\n\n representations_by_property[property_name] = property_types\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 ancestor_key = datastore.key \"__kind__\", \"Task\"\n query = datastore.query(\"__property__\")\n .ancestor(ancestor_key)\n\n entities = datastore.run query\n representations = entities.each_with_object({}) do |entity, memo|\n property_name = entity.key.name\n property_types = entity[\"property_representation\"]\n memo[property_name] = property_types\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)."]]