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

huangapple go评论75阅读模式
英文:

Does datastore require an ancestor to be specified when querying root and children entities of the same type?

问题

例如,从提供的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
}

如果将查询更改为不包括.Ancestor(defaultTodoList(c)).,该函数将无法返回任何待办事项结果。

  • 如果实体保存了祖先,必须通过该祖先进行查询吗?
  • 如何查询与它们是子实体还是根实体无关的实体?
  • 在建模应用程序时,需要进行无祖先查询时,应考虑哪些性能和架构问题?
英文:

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(&quot;Todo&quot;).Ancestor(defaultTodoList(c)).Order(&quot;Created&quot;).GetAll(c, &amp;todos)
	if err != nil {
		return nil, err
	}
	for i := 0; i &lt; 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?

答案1

得分: 1

如果一个实体被保存时带有祖先信息,你是否必须通过该祖先来查询它?

不,你不必这样做。

如何查询实体,无论它们是子实体还是根实体?

从这里的一个简单示例开始:

// 假设c是你的App Engine上下文
q = datastore.NewQuery("MyObject") // 如果需要,可以在这里添加过滤器和排序。

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

我还在这里找到了这个注释:

注意:设置祖先过滤器可以进行强一致性查询。没有祖先过滤器的查询只返回最终一致性的结果。

这很重要,因为我相信现在所有的数据存储都是高复制的。你可以在这里阅读更多关于高复制数据存储的信息。

英文:

> 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(&quot;MyObject&quot;) // Can add filters and sorting here if desired.

for t := q.Run(c);; {
    var x MyObject
    key, err := t.Next(&amp;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.

答案2

得分: 0

defaultTodoList(c)可能不包含代码所期望的父实体。在使用之前,尝试通过将其存储在变量中,然后通过日志记录来验证其值。

英文:

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.

huangapple
  • 本文由 发表于 2014年3月21日 07:41:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/22547442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定