如何构建GQL以便不包含集合中的值? [英] How to construct GQL to not contain a value from a set?

查看:127
本文介绍了如何构建GQL以便不包含集合中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Google应用程序引擎db中选择 db.Model 对象的键不在给定列表中的位置?如果是这样的话,语法是什么?



模型类的例子:

  class Spam(db.Model):
field1 = db.BooleanProperty(default = false)
field2 = db.IntegerProperty()
pre>

我想工作但无法解决的查询示例:

<$ p $ spam_results = db.GqlQuery(
SELECT * FROM Spam WHERE key NOT IN:1 LIMIT 10,
['ag1waWNreXByZXNlbnRzchMLEgxBbm5vdW5jZW1lbnQYjAEM',
'ag1waWNreXByZXNlbnRzchMLEgxBbm5vdW5jZW1lbnQYjgEM'] )

for spam_results中的鸡蛋:
printid:%s%a.key().id()


解决方案

否尽管app引擎支持IN查询,但它不支持NOT IN查询。



然而,如果您不需要的实体列表很小,那么您可能只需检索每个实体并过滤掉你自己不需要的那些。



交替反之,如果要排除的实体列表是所有实体的很大一部分,那么上述解决方案效率会非常低。相反,也许你可以添加一个额外的属性到你的模型中,你可以使用它来过滤掉你不想要的实体(不管这是否可能取决于你的具体需求和数据)。


Is it possible to select from a google app engine db where the key of a db.Model object is not in a given list? If so, what would be the syntax?

Ex of a model class:

class Spam(db.Model):
    field1 = db.BooleanProperty(default=false)
    field2 = db.IntegerProperty()

Example of a query which I'd like to work but can't figure out:

spam_results = db.GqlQuery(
"SELECT * FROM Spam WHERE key NOT IN :1 LIMIT 10", 
['ag1waWNreXByZXNlbnRzchMLEgxBbm5vdW5jZW1lbnQYjAEM', 
 'ag1waWNreXByZXNlbnRzchMLEgxBbm5vdW5jZW1lbnQYjgEM'])

for eggs in spam_results:
  print "id: %s" % a.key().id()

解决方案

No Though app engine supports an "IN" query, it does not support a "NOT IN" query.

However, if your list of entities you don't want is small, then you might as well just retrieve every entity and filter out the ones you don't need yourself.

Alternatively, if the list of entities you want to exclude is a large fraction of all entities, then the above solution would be rather inefficient. Instead, perhaps you could add an additional property to your model which you could use to filter out entities you don't want (whether or not this is possible will depend on your specific needs and data).

这篇关于如何构建GQL以便不包含集合中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆