Using "IN Array" Queries in Google App Engine Datastore with Go
Querying by ID Array
Q: How can I perform a query on the Datastore that includes an array of IDs?
A: The Datastore does not natively support "IN" queries.
Multiple Queries
A workaround is to execute separate queries for each element in the ID array. Alternatively, if the IDs are in a continuous range, you can use the ">=" and "<=" operators:
<code class="go">ids := []int64{1, 2, 3, 4} q := datastore.NewQuery("Category").Filter("Id>=", 1).Filter("Id<=", 4)</p> <p><strong>GetMulti</strong></p> <p>For queries on the entity key property, you can use the datastore.GetMulti() function:</p> <pre class="brush:php;toolbar:false"><code class="go">var keys []*datastore.Key for _, id := range ids { keys = append(keys, datastore.NewKey(c, "Category", "", id, nil)) } categories := make([]Category, len(keys)) err := datastore.GetMulti(c, keys, categories)</code>
Filter Behavior
Note that multiple Query.Filter() calls will result in an AND connection between filters. This may yield unexpected results if you expect an OR connection. Ensure that you store the returned query and use it as the basis for subsequent filters:
<code class="go">q := q.Filter("Id=", id)</code>
The above is the detailed content of Here are a few title options, combining the Q&A format with a focus on the problem and solution: Direct and Concise: * How to Query by ID Array in Google App Engine Datastore (Go)? * Datastore Q. For more information, please follow other related articles on the PHP Chinese website!