【问题标题】:ndb projection query return incorrect number of entitiesndb 投影查询返回不正确的实体数量
【发布时间】:2018-08-25 12:30:03
【问题描述】:

我的 TwAccount 是

class TwAccount(ndb.Model):

   100 different properties here
   error = ndb.IntegerProperty(repeated=True)

我试试:

twaccount_dbs = model.TwAccount.query().filter(ndb.GenericProperty('followuserfollowme') == True)

它返回 1 个实体

但我只想查询 1 个属性。

twaccount_dbs = model.TwAccount.query().filter(ndb.GenericProperty('followuserfollowme') == True).fetch(projection=["error"])

然后它返回 0 个实体。

我试试

twaccount_dbs = model.TwAccount.query().filter(ndb.GenericProperty('followuserfollowme') == True).fetch(projection=[model.TwAccount.error])

但它也返回 0 个实体

我希望它返回 1 个实体。

更新 1: 我发现如果错误是一个 emtpy(所以它不存在),那么投影查询将返回 0

我的目标是查询 TwAccount 中的所有实体。如果 error 为空,则执行 deferred.defer(function,entity_key)。

我想使用投影查询来节省阅读成本。不可能吗?

【问题讨论】:

  • 您的TwAccount.error 属性是否已编入索引?
  • 是的,它已编入索引。我更新了我的问题。我发现如果错误是一个 emtpy(所以它不存在),那么投影查询将返回 0。我的目标是查询 TwAccount 中的所有实体。如果错误为空,则执行 deferred.defer(function,entity_key)。我想使用投影查询来节省阅读成本。不可能吗?

标签: google-app-engine google-cloud-datastore app-engine-ndb


【解决方案1】:

很遗憾,您无法通过未设置/空属性过滤查询结果:

来自Index definition and structure

仅当实体具有索引值集时,它才会包含在索引中 对于索引中使用的每个属性;如果索引定义引用 对于实体没有价值的属性,该实体不会 出现在索引中,因此永远不会作为结果返回 基于索引的任何查询。

另见相关AppEngine: Query datastore for records with <missing> value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多