Go + App Engine Datastore:如何过滤掉为空的行?

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

Go + App Engine Datastore: How to filter out rows that are null?

问题

如何过滤掉空行?我知道找到只有空行很困难,但希望这样做会更容易。

我想要做以下操作:

q := datastore.NewQuery("MY_KIND").Filter("MY_ID !=", nil)

... 但是Filter不支持!=比较符。顺便说一下,在Datastore Viewer中使用这个GQL语法是可以正常工作的:

SELECT * FROM MY_KIND WHERE MY_ID != NULL
英文:

How do I filter out rows that are null? I know it's hard to find only-null rows, but hopefully this should be easier.

I'd like to do the following:

q := datastore.NewQuery("MY_KIND").Filter("MY_ID !=", nil)

... but Filter doesn't support the != comparator. FYI, using this GQL syntax in the Datastore Viewer works just fine:

SELECT * FROM MY_KIND WHERE MY_ID != NULL

答案1

得分: 2

你可以使用greater过滤器,并设置适当的值(对于数字,设置为> 0;对于字符串,设置为> "")。

通常情况下,ID不能是空字符串或零。

英文:

You can use greater filter with the appropriate value (> 0 for numbers, > "" for strings).

Typically an ID cannot be an empty string or zero.

答案2

得分: 0

这是对我有效的方法。

// 数据存储实体的空值
// 在我的情况下,DeletedAt 是 *time.Time 类型
{
"DeletedAt": NULL,
"Version": 1,
...
}

这是我用来获取 DeletedAtNULL 的记录的查询语句:

datastore.NewQuery("MyKind").Filter("DeletedAt =", (*time.Time)(nil))

希望对你们中的某些人有所帮助。

英文:

Here's what works for me.

// Datastore entity with null value
// `DeletedAt` in my case is *time.Time
{
  "DeletedAt": NULL,
  "Version": 1,
  ...
}

Here's my query to get records where DeletedAt is NULL:

datastore.NewQuery("MyKind").Filter("DeletedAt =", (*time.Time)(nil))

Hope that this will be helpful to someone of you.

huangapple
  • 本文由 发表于 2014年8月12日 00:12:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/25247747.html
匿名

发表评论

匿名网友

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

确定