谷歌应用引擎(Google App Engine)Datastore中使用“in”运算符的Go查询

huangapple go评论87阅读模式
英文:

google app engine datastore Go Query with "in" operator

问题

在App Engine Datastore中使用查询,如何指定获取与具有可变值的属性匹配的键?

var Person struct {
   name string
   department string  
}

//Query
q := datastore.NewQuery("Person").Filter("department =", "department1").KeysOnly()

在上面的查询中,我想要使用**"IN"**运算符来指定多个部门值,即获取属于department1、department2、department3等部门的所有人员键。

这是否可以通过一个查询实现?还是我需要为每个部门进行一个查询?

英文:

Using Query in App Engine Datastore, how do I specify to fetch keys that match a property with variable values?

<!-- language: go -->

var Person struct {
   name string
   department string  
}

//Query
q := datastore.NewQuery(&quot;Person&quot;).Filter(&quot;department = &quot;, &quot;department1&quot;).KeysOnly()

In the above query, instead of "=" operator, I want "IN" operator to specify more than 1 department value i.e fetch all person keys who belong to department1, department2, department3 etc.

Is this possible with 1 Query? Or do I need to make 1 query for each department?

答案1

得分: 2

其他运行时环境允许使用“IN”运算符进行数据存储查询。然而,这只是一种便利方式:在底层,数据存储为列表中的每个元素进行单独的查询。

如果您只有相对较少的实体,可能更高效的方法是检索所有实体,然后根据“department”属性过滤结果,而不是发出N个查询来搜索N个可能的部门。

参考链接:http://gae-java-persistence.blogspot.com/2009/12/queries-with-and-in-filters.html

英文:

Other runtimes allow "IN" operator for datastore queries. It is, however, just a convenience: under the hood, the datastore makes individual queries for each element in the list.

If you have a relatively small number of entities, it may be more efficient to retrieve all of them and then filter the results based on the "department" property, rather than issues N queries to search for N possible departments.

huangapple
  • 本文由 发表于 2016年1月28日 02:02:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/35044782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定