Is there a way to do a query with ids []int64
on datastore? I've tried the following with no avail.
Errors out
q := datastore.NewQuery("Category").Filter("Id IN", ids)
Just gets me all the the categories in the datastore
for _, id := range ids {
q.Filter("Id =", id)
}
After icza's answer
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
}
Datastore mode indexes each unique array property value once per index. Thus to query if an array contains a value use an equality filter. Consider the following when your query includes properties with array values.
A query retrieves entities from Cloud Firestore in Datastore mode that meet a specified set of conditions. The query operates on entities of a given kind; it can specify filters on the entities' property values, keys, and ancestors, and can return zero or more entities as results.
A query retrieves entities from Firestore in Datastore mode that meet a specified set of conditions. The query operates on entities of a given kind ; it can specify filters on the entities' property values, keys, and ancestors, and can return zero or more entities as results.
This tutorial help to check an element exists or not in the golang array. You’ll have to develop your own function because Golang doesn’t have a generic method for this. We have created isElementExist functioN, This function will take arguments as a parameter : string : The array of source strings. str : The element to whome be search into source.
Generally "IN"
filters are not supported by the Datastore. The documentation of Query.Filter()
lists the allowed operators:
">", "<", ">=", "<=", or "="
What you can do is execute a separate query for each of the elements in the array you want to filter by. Also if the elements are in a continous range, you can substitute the IN
with id>=min
and id<=max
. E.g.:
ids := []int64{1,2,3,4}
q := datastore.NewQuery("Category").Filter("Id>=", 1).Filter("Id<=", 4)
Also note that while the IN
is not supported in general, if the property is the entity key itself, you can get a list of entities specified by an array of their keys using the datastore.GetMulti()
function:
func GetMulti(c appengine.Context, key []*Key, dst interface{}) error
Note:
Your 2nd attempt returns all entities because you call Filter()
on your query, but you don't store the return value, so the query you end up executing will have no filters at all. Query.Filter()
returns a derivative query which contains the filter you just specified, you have to use the returned Query
ongoing. So it should be:
q = q.Filter("Id=", id)
But even this won't work either: if multiple filters are specified, they will be in logical AND connection so it will most likely give you 0 results as I suspect no category will exists where Id
is a list and which would contain all the ids you want to filter by.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With