做一个“IN阵列”用golang查询谷歌应用程序引擎数据存储 [英] Doing a "IN Array" query on google app engine datastore with golang
问题描述
有没有办法在数据存储上使用 ids [] int64
执行查询?
$ b
-
错误输出
< pre $q:= datastore.NewQuery(Category)。Filter(Id IN,ids)
-
只需将数据存储中的所有类别都带到我的列表中即可。
$ b
,id:= range ids {
q.Filter(Id =,id)
}
经过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
}
数据存储不支持通常IN
过滤器。 Query.Filter()
列出了允许的操作符:
>,<, > =,< =或=
您可以执行的是执行为要过滤的数组中的每个元素分别查询一个查询。此外,如果元素处于连续范围内,可以用 id> = min
和<$ c替换 IN
$ C> ID< =最大。例如:
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 {})错误
注意:
您的第二次尝试返回所有实体,因为您在查询中调用了 Filter()
,但不存储返回值,所以您最终执行的查询根本没有过滤器。 Query.Filter()
返回包含您刚才指定的过滤器的派生查询,您必须使用返回的 Query
正在进行中。所以应该是:
q = q.Filter(Id =,id)
但是即使这样做也行不通:如果指定了多个过滤器,它们将处于逻辑AND连接状态,所以它很可能会给你0的结果,因为我怀疑没有类别存在,其中 Id
是一个列表,其中包含您要过滤的所有id。
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
}
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.
这篇关于做一个“IN阵列”用golang查询谷歌应用程序引擎数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!