10

Is there an efficient mechanism for querying by the number of items in a repeated property in NDB?

I'd like to do something like:

Class.query(class.repeated_property.count == 2)

but of course this doesn't work.

KitB
  • 510
  • 2
  • 16

2 Answers2

25

Specifically, you can use ComputedProperty to automatically store the count, e.g.

class X(ndb.Model):
  prop = ndb.StringProperty(repeated=True)
  prop_count = ndb.ComputedProperty(lambda e: len(e.prop))

X.query(X.prop_count == 2)
systempuntoout
  • 71,966
  • 47
  • 171
  • 241
Guido van Rossum
  • 16,690
  • 3
  • 46
  • 49
3

There is no len query semantic in GQL, you will need to have a sperate property for the length of the list and query on it.

Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87