Reputation: 991
Scenario 1: If I have a Great Grandpa ancestor (let's name him A); And my key is from "child1"; is there a way to check that my Great Grandpa is A? (hope I can do that without needing to loop)
Or can I check, if child1's key is of the path "A->B->C".
A -> B -> C -> (child1, child2...)
Scenario 2: From the above. Great Grandpa has another descendants from "G", and would like to retrieve "H"s children:
A-> B -> C -> (children of C)
...-> G -> H -> (children of H)
I like to retrieve "H"s children, thinking that Grandpa knows the path from A, G, to H... can I do that? (hope I can do this in a query, without looping)
If you have a Go1 example: that would be awesome...
Upvotes: 1
Views: 739
Reputation: 66
Scenario 1:
If you want to check that Child 1's great-grandpa is A, you will have to invoke key.getParent() twice (checking for null parents). There is no API to check this for you.
If you want to check that entity X has has A as an ancestor then you will have to call key.getParent() N times.
Note however the overhead is minimal. Calling key.getParent() does not result in any calls to the actual datastore.
You can of course ensure with an ancestor query that C / entity X is a descendant of A (as your scenario 2 implies). Thus avoiding checking the query result. The datastore on query execution will check this for you.
https://developers.google.com/appengine/docs/java/datastore/queries => search for Ancestor Queries https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Query#setAncestor(com.google.appengine.api.datastore.Key)
childCQuery.setAncestor(entityA.getKey());
Scenario 2:
Grandpa 'A' can't know the path to 'H' since children can be added and removed at any point in time. There is no limitation on what entities can be descendants of 'A'. So only with a datastore query can you determine the descendants of 'A'.
But as stated in scenario 1 you can specify 'A' as the ancestor in your query so that you filter any results where 'A' is not the ancestor.
Hope this answers your questions.
Note: My responses to your question refer to the java API. I am not yet familiar with the Go API.
Thanks.
Amir
Upvotes: 2