英文:
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("Person").Filter("department = ", "department1").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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论