Running an "IN Array" query in google app engine datastore using golang

Is there any way to make a query with ids []int64in the data warehouse? I tried the following to no avail.

  • Mistakes

    q := datastore.NewQuery("Category").Filter("Id IN", ids)
    
  • Just gets me all the categories in the data warehouse

    for _, id := range ids {
        q.Filter("Id =", id)
    }
    

After the answer icza

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
}
+4
source share
1 answer

Typically, filters are "IN"not supported by the data warehouse. The documentation Query.Filter()lists the valid operators:

">", "<", ">=", "<=", or "="

, , . , , IN id>=min id<=max. :.

ids := []int64{1,2,3,4}
q := datastore.NewQuery("Category").Filter("Id>=", 1).Filter("Id<=", 4)

, IN , , , , datastore.GetMulti():

func GetMulti(c appengine.Context, key []*Key, dst interface{}) error

:

, Filter() , , , , . Query.Filter() , , Query . :

q = q.Filter("Id=", id)

: , , , , 0 , , , Id - , .

+4

All Articles