Performing an "IN Array" query, where multiple values are checked for a specific property in Datastore, requires a different approach in Go due to the lack of native support for this type of query.
One option is to execute separate queries for each element in the array you want to filter by. For example:
<code class="go">ids := []int64{1, 2, 3, 4} for _, id := range ids { q := datastore.NewQuery("Category").Filter("Id =", id) // Execute the query and process the results for each element }</code>
If the elements in the array form a continuous range, you can use the >= and <= operators to substitute the "IN" filter. For instance:
<code class="go">ids := []int64{1, 2, 3, 4} q := datastore.NewQuery("Category").Filter("Id >=", 1).Filter("Id <=", 4)</p> <h3>3. datastore.GetMulti()</h3> <p>If the property to be filtered by is the entity key itself, you can retrieve a list of entities based on an array of their keys using 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) if err != nil { return nil, err }</code>
Note: Remember that compound filters using AND may return unexpected results, as multiple filters will be applied logically as such.
The above is the detailed content of How to Execute \'IN Array\' Queries in Google App Engine Datastore with Go?. For more information, please follow other related articles on the PHP Chinese website!