Search code examples
google-app-enginegoogle-cloud-datastoreapp-engine-ndb

ndb projection query return incorrect number of entities


My TwAccount is

class TwAccount(ndb.Model):

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

I try:

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

it returns 1 entity

But I only want to query 1 property.

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

then it returns 0 entity.

I try

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

but it also returns 0 entity

I expect it returns 1 entity.

Update 1: I figure out that if error is an emtpy (so that does not exist), then the projection query will return 0

My objective is to query all entities in TwAccount. If error is empty, then do deferred.defer(function,entity_key).

I want to use projection query to save the cost of reading. Is it impossible?


Solution

  • Unfortunately you cannot filter query results by an unset/empty property:

    From Index definition and structure:

    An entity is included in the index only if it has an indexed value set for every property used in the index; if the index definition refers to a property for which the entity has no value, that entity will not appear in the index and hence will never be returned as a result for any query based on the index.

    See also related AppEngine: Query datastore for records with <missing> value