KitB
KitB

Reputation: 510

Querying a repeated property by count in NDB

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.

Upvotes: 10

Views: 1781

Answers (2)

Guido van Rossum
Guido van Rossum

Reputation: 16890

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)

Upvotes: 25

Shay Erlichmen
Shay Erlichmen

Reputation: 31928

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.

Upvotes: 3

Related Questions