How to query for entities using a list of ancestor keys

1.2k views Asked by At

I have the following data model in Google Cloud Datastore:

Parent:
  - name

Child:
  - ancestor: Parent
  - name

I have a list of Parent keys. How can I query for all Child entities descending from the list of Parent keys ?

I have tried

SELECT * FROM Child WHERE __key__ HAS ANCESTOR Key(Parent, 'abc'), Key(Parent, 'dfe')

and

SELECT * FROM Child WHERE __key__ HAS ANCESTOR [Key(Parent, 'abc'), Key(Parent, 'dfe')]

and neither select statements work.

1

There are 1 answers

1
Ed Davisson On BEST ANSWER

Cloud Datastore queries can operate on at most one ancestor, so you'd have to run this as two queries:

SELECT * FROM Child WHERE __key__ HAS ANCESTOR Key(Parent, 'abc')
SELECT * FROM Child WHERE __key__ HAS ANCESTOR Key(Parent, 'dfe')

and merge the results.

Or if you can get away with fetching all Child entities, you could run:

SELECT * FROM Child