简体   繁体   中英

How to query for direct childs of entity in GAE Datastore(HRD)?

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?

try:

X.all().filter("parent = ", y)

Using the Datastore Query() you can set its ancestor by using the method setAncestor() but it does not guarantee that the ancestor is the direct parent.

What you can do to ensure fetching only the direct children is by doing a comparison operation.

if( directChildEntity.getKey().getParent().equals( directParentEntity.getKey() ) )
        {
        // directChildEntity is a direct child of directParentEntity
        }

The trick is to use the Datastore Key's getParent() method since it can mediate a one-step hierarchy between keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM