Michael D. Moffitt
Michael D. Moffitt

Reputation: 781

Go + App Engine Datastore: How to filter out rows that are 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

Upvotes: 1

Views: 927

Answers (2)

tsg
tsg

Reputation: 485

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.

Upvotes: 0

Andrei Volgin
Andrei Volgin

Reputation: 41099

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

Typically an ID cannot be an empty string or zero.

Upvotes: 2

Related Questions