英文:
Projection query with new fields/properites ignores entries that haven't set those properties yet
问题
我有一个名为Article
的结构体,结构如下:
type Article struct {
Title string
Content string `datastore:",noindex"`
}
在我的网站的管理部分,我列出了所有的文章。为了显示这个列表,我只需要Title
属性;获取文章内容似乎是浪费的。因此,我使用了投影查询:
q := datastore.NewQuery("Article").Project("Title")
到目前为止,一切都按预期工作。现在,我决定给Article
添加两个字段,以便某些文章在公共文章列表中不显示,或者在尝试访问时无法查看。理解数据存储为无模式,我认为这可能非常简单。我将这两个新字段添加到Article
中:
type Article struct {
Title string
Content string `datastore:",noindex"`
Unlisted bool
Unviewable bool
}
我还将它们添加到投影查询中,因为我希望在管理文章列表中指示文章是否公开未列出或无法查看:
q := datastore.NewQuery("Article").Project("Title", "Unlisted", "Unviewable")
不幸的是,这只返回了在将数据放入数据存储时明确包含了Unlisted
和Unviewable
的条目。
我目前的解决方法是简单地停止使用投影查询:
q := datastore.NewQuery("Article")
所有条目都会被返回,并且从未设置Unlisted
或Unviewable
的条目会按预期将它们设置为零值。缺点是文章内容被不必要地传递。
在这种情况下,这种妥协并不糟糕,但我预计将来可能会出现类似的情况,不能使用投影查询可能会成为一个大问题。投影查询和向数据存储条目添加新属性似乎不太匹配。我想确保我没有误解什么或者漏掉了正确的做法。
-
从文档中我无法清楚地了解到投影查询应该是这样工作的(忽略没有投影属性的条目而不是将它们包含在内并赋予零值)。这是预期的行为吗?
-
在这种情况下(向结构体添加新字段/向数据存储条目添加新属性),唯一的选择是放弃投影查询或运行某种“模式迁移”,即
Get
所有条目,然后将它们Put
回去,这样它们就具有零值属性并且可以进行投影查询吗?
英文:
I have an Article
type structured like this:
type Article struct {
Title string
Content string `datastore:",noindex"`
}
In an administrative portion of my site, I list all of my Articles. The only property I need in order to display this list is Title
; grabbing the content of the article seems wasteful. So I use a projection query:
q := datastore.NewQuery("Article").Project("Title")
Everything works as expected so far. Now I decide I'd like to add two fields to Article
so that some articles can be unlisted in the public article list and/or unviewable when access is attempted. Understanding the datastore to be schema-less, I think this might be very simple. I add the two new fields to Article
:
type Article struct {
Title string
Content string `datastore:",noindex"`
Unlisted bool
Unviewable bool
}
I also add them to the projection query, since I want to indicate in the administrative article list when an article is publicly unlisted and/or unviewable:
q := datastore.NewQuery("Article").Project("Title", "Unlisted", "Unviewable")
Unfortunately, this only returns entries that have explicitly included Unlisted
and Unviewable
when Put
into the datastore.
My workaround for now is to simply stop using a projection query:
q := datastore.NewQuery("Article")
All entries are returned, and the entries that never set Unlisted
or Unviewable
have them set to their zero value as expected. The downside is that the article content is being passed around needlessly.
In this case, that compromise isn't terrible, but I expect similar situations to arise in the future, and it could be a big deal not being able to use projection queries. Projections queries and adding new properties to datastore entries seem like they're not fitting together well. I want to make sure I'm not misunderstanding something or missing the correct way to do things.
-
It's not clear to me from the documentation that projection queries should behave this way (ignoring entries that don't have the projected properties rather than including them with zero values). Is this the intended behavior?
-
Are the only options in scenarios like this (adding new fields to structs / properties to entries) to either forgo projection queries or run some kind of "schema migration",
Get
ting all entries and thenPut
ing them back, so they now have zero-valued properties and can be projected?
答案1
得分: 1
投影查询从索引中获取字段的数据,而不是从实体中获取。当你添加了新的属性时,预先存在的记录不会出现在你执行投影查询的索引中。它们需要重新索引。
你正在请求那些特定的属性,但它们不存在,因此出现了当前的行为。
你应该将投影查询视为对具有请求索引中的值的实体的请求,除了你在查询中放置的任何过滤器之外。
英文:
Projection queries source the data for fields from the indexes not the entity, when you have added new properties pre-existing records do not appear in those indexes you are performing the project query on. They will need to be re-indexed.
You are asking for those specific properties and they don't exist hence the current behaviour.
You should probably think of a projection query as a request for entities with a value in a requested index in addition to any filter you place on a query.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论