查询相同类型的根实体和子实体时,数据存储区是否需要指定祖先?

For example, take the following snippet from the provided appengine-angular-gotodos :

func getAllTodos(c appengine.Context) ([]Todo, error) {
    todos := []Todo{}
    ks, err := datastore.NewQuery("Todo").Ancestor(defaultTodoList(c)).Order("Created").GetAll(c, &todos)
    if err != nil {
        return nil, err
    }
    for i := 0; i < len(todos); i++ {
        todos[i].Id = ks[i].IntID()
    }
    return todos, nil
}

If you change the query to not include .Ancestor(defaultTodoList(c)). the function fails to return any todo results.

  • If an entity is saved with an ancestry, must you query it by that ancestor?
  • How do you query for Entities regardless of whether they are children, or root entities?
  • What are the performance and architecture considerations I should make when modeling my app given the need to do ancestor-less queries?

defaultTodoList(c) might not contain the parent Entity that the code expects it to contain. Try to verify its value somehow before using it, for example by storing it in a variable and then logging data out of it.

If an entity is saved with an ancestry, must you query it by that ancestor?

No you don't have to.

How do you query for Entities regardless of whether they are children, or root entities?

Reduced to as simple an example as possible from here:

//Assuming c is your appengine context
q = datastore.NewQuery("MyObject") // Can add filters and sorting here if desired.

for t := q.Run(c);; {
    var x MyObject
    key, err := t.Next(&x)
}

I also found this note here:

Note: Setting an ancestor filter allows for strongly consistent queries. Queries without an ancestor filter only return eventually consistent results.

This is important because I believe all datastores are HRD now. You can read more about high replication datastores.