Property filter query
Stay organized with collections
Save and categorize content based on your preferences.
Use a property filter 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 page demonstrates how to use a property filter query within the Datastore mode, showcasing implementations in C#, Java, PHP, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample uses a \u003ccode\u003e__key__\u003c/code\u003e filter to retrieve properties based on a specified \u003ccode\u003estartKey\u003c/code\u003e, which is generated using a specific \u003ccode\u003e__kind__\u003c/code\u003e and \u003ccode\u003e__property__\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe examples show how to construct and run a query that filters data based on the property's key, such that the key is greater than or equal to the \u003ccode\u003estartKey\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe various implementations collect and display properties associated with a specific kind, effectively demonstrating how to extract and organize property metadata.\u003c/p\u003e\n"],["\u003cp\u003eThe page references relevant documentation, including client library usage and authentication setup for a local development environment.\u003c/p\u003e\n"]]],[],null,["# Property filter query\n\nUse a property filter query.\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 Key startKey = new KeyFactory(key, \"__property__\")\n .CreateKey(\"priority\");\n Query query = new Query(\"__property__\")\n {\n Filter = Filter.GreaterThanOrEqual(\"__key__\", startKey)\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 properties.Add($\"{kind}.{property}\");\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 Key startKey =\n datastore\n .newKeyFactory()\n .setKind(\"__property__\")\n .addAncestors(PathElement.of(\"__kind__\", \"Task\"))\n .newKey(\"priority\");\n Query\u003cKey\u003e query =\n Query.newKeyQueryBuilder()\n .setKind(\"__property__\")\n .setFilter(PropertyFilter.ge(\"__key__\", startKey))\n .build();\n Map\u003cString, Collection\u003cString\u003e\u003e propertiesByKind = new HashMap\u003c\u003e();\n QueryResults\u003cKey\u003e keys = datastore.run(query);\n while (keys.hasNext()) {\n Key key = keys.next();\n String kind = key.getParent().getName();\n String propertyName = key.getName();\n Collection\u003cString\u003e properties = propertiesByKind.get(kind);\n if (properties == null) {\n properties = new HashSet\u003cString\u003e();\n propertiesByKind.put(kind, properties);\n }\n properties.add(propertyName);\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 $startKey = $datastore-\u003ekey('__property__', 'priority')\n -\u003eancestorKey($ancestorKey);\n $query = $datastore-\u003equery()\n -\u003ekind('__property__')\n -\u003efilter('__key__', '\u003e=', $startKey);\n $result = $datastore-\u003erunQuery($query);\n /* @var array\u003cstring\u003e $properties */\n $properties = [];\n /* @var Entity $entity */\n foreach ($result as $entity) {\n $kind = $entity-\u003ekey()-\u003epath()[0]['name'];\n $propertyName = $entity-\u003ekey()-\u003epath()[1]['name'];\n $properties[] = \"$kind.$propertyName\";\n }\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 start_key = datastore.key [[\"__kind__\", \"Task\"], [\"__property__\", \"priority\"]]\n query = datastore.query(\"__property__\")\n .select(\"__key__\")\n .where(\"__key__\", \"\u003e=\", start_key)\n\n entities = datastore.run query\n properties_by_kind = entities.each_with_object({}) do |entity, memo|\n kind = entity.key.parent.name\n prop = entity.key.name\n memo[kind] ||= []\n memo[kind] \u003c\u003c prop\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)."]]