andrew andrew - 4 months ago 18
Python Question

Appengine - ndb query with unknown list size

I have an appengine project written in Python.
I use a model with a

tags = ndb.StringProperty(repeated=True)

What I want is, given a list of tags, search for all the objects that have every tag in the list.
My problem is that the list may contain any number of tags.

What should I do?


When you make a query on a list property, it actually creates a set of subqueries at the datastore level. The maximum number of subqueries that can be spawned by a single query is 30. Thus, if your list has more that 30 elements, you will get an exception.

In order to tackle this issue, either you will have to change your database model or create multiple queries based on the number of list elements you have and then combine the results. Both these approaches need to be handled by your code.

Update: In case you need all the tags in the list to match the list property in your model, then you can create your basic query and then append AND operators in a loop (as marcadian describes). For example:

qry = YourModel.query()
qry = qry.filter(YourModel.tags == tag[i]) for enumerate(tags)

But, as I mentioned earlier you should be careful of the length of the list property in your model and your indexes configuration in order to avoid problems like index explosion. For more information about this, you may check: