We can get all children of y (including indirect), by X.all().ancestor(y), but I want to receive only those which are direct children of y.
Is there any way to do it?
We can get all children of y (including indirect), by X.all().ancestor(y), but I want to receive only those which are direct children of y.
Is there any way to do it?
尝试:
X.all().filter("parent = ", y)
使用 Datastore Query() ,您可以使用setAncestor()方法设置其祖先,但它不保证祖先是直接父级。
您可以通过执行比较操作来确保仅获取直接子级。
if( directChildEntity.getKey().getParent().equals( directParentEntity.getKey() ) )
{
// directChildEntity is a direct child of directParentEntity
}
诀窍是使用 Datastore Key 的 getParent()方法,因为它可以调解键之间的一步层次结构。