Home  >  Article  >  Backend Development  >  How to Query Datastore for Prefixed Strings in Google App Engine?

How to Query Datastore for Prefixed Strings in Google App Engine?

Barbara Streisand
Barbara StreisandOriginal
2024-10-24 07:04:02839browse

How to Query Datastore for Prefixed Strings in Google App Engine?

Searching for Prefixed Strings in Google App Engine Datastore

Querying Google App Engine Datastore to retrieve entities based on a prefix can be achieved through a combination of inequality filters.

To search for all entities where the "Name" property begins with a specific string, use a GQL query as follows:

SELECT * FROM Places WHERE Name > 'prefix' AND Name < 'prefix' + '\xFF'

Alternatively, in Go code, the query can be expressed as:

q := datastore.NewQuery("Places").Filter("Name >", "prefix").Filter("Name <", "prefix" + "\xFF")

This approach ensures that the query retrieves only entities with names greater than (or equal to) the specified prefix and less than the lexicographically next string in sequence. For example, for the prefix "li," it will match names like "liam," "lisotto," and "lizst" but exclude names like "abc," "ljoi," or "qwerty."

Note that the query is case-sensitive, meaning that "List" and "li" are considered distinct values in the lexicographical ordering.

The above is the detailed content of How to Query Datastore for Prefixed Strings in Google App Engine?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn