Home >Backend Development >Golang >Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?

Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?

DDD
DDDOriginal
2024-10-24 07:08:021055browse

Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?

Search Entities with Prefixed Names in Google App Engine Datastore

Question:

Is it feasible to retrieve entities from Datastore where a specific field begins with a given string? Despite attempting the following query, I am unable to obtain the desired results:

q = datastore.NewQuery("Places").Filter("Name >", "a")

Answer:

Querying entities based on a field prefix is indeed supported in Datastore, but it necessitates the conjunction of two inequality filters.

For instance, to retrieve Places with names starting with "li," the query should specify that the Name field is greater than (or equal to) "li" and less than "lj." This is because "lj" is the lexicographically subsequent prefix.

In GQL, this query would appear as:

SELECT * FROM Places WHERE Name > 'li' AND Name < 'lj'

In Go code, the query takes the form:

q = datastore.NewQuery("Places").Filter("Name >", "li").Filter("Name <", "lj")

This query will yield Places with names like:

liam
lisotto
lizst

However, it will exclude names resembling:

abc
ljoi
lj
qwerty

Note that upper- and lowercase letters have distinct lexicographical orders. Thus, "List" precedes "li," while "list" follows "li."

The above is the detailed content of Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?. 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