Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I query for direct descendants only?

Let's say I have entities a, b and c all of the same type, and the situation is like this:

entity a is parent for entity b entity b is parent for entity c

Now if I do the following query

query = ndb.Query(ancestor=a.key)
result = query.fetch()

The result will contain both b and c entities. Is there a way I can filter out c so that only entities that are direct descendants remain? Any way apart from me going through the results and removing them I mean.

like image 815
sorin.silaghi Avatar asked Apr 18 '12 22:04

sorin.silaghi


2 Answers

The only way to do this is to modify your schema, adding a 'parent' KeyProperty that references an entity's direct parent, then filtering on that.

like image 176
Nick Johnson Avatar answered Nov 20 '22 00:11

Nick Johnson


Actually, this is not supported at all. Nick's answer does work but only if you can specify the entity kind in your query which the OP did not specify:

"Kindless queries cannot include filters on properties. They can, however, filter by Entity Key by passing Entity.KEY_RESERVED_PROPERTY as the property name for the filter. Ascending sorts on Entity.KEY_RESERVED_PROPERTY are also supported."

like image 30
Mo'in Creemers Avatar answered Nov 20 '22 01:11

Mo'in Creemers