1

In my collection I have several documents where the "token" field is null.

query := client.Collection("records").Where("token", "==", nil)

In Go, the query above returns zero documents.

What is the correct query to fetch all documents where the "token" field is null or missing?

Daniele B
  • 19,801
  • 29
  • 115
  • 173
  • You should test against null (without quotation marks) if they are actually null in the database, not nil, as in .where("foo", "==", null); – Stacking For Heap Feb 28 '18 at 22:47
  • 1
    I'm not too familiar with go, but I would expect this to work, as it does similarly in other languages. There might be a bug in the client library. Please feel free to file a bug. https://firebase.google.com/support/contact/bugs-features/ – Doug Stevenson Feb 28 '18 at 22:56
  • @DougStevenson thanks, I just filed a bug report – Daniele B Feb 28 '18 at 23:00
  • Can you actually show a screenshot with a document where the field is `null`? I want to make sure the field is present-but-null, and not missing altogether. – Frank van Puffelen Feb 28 '18 at 23:28
  • @FrankvanPuffelen, how can you fetch with a query both missing and present-but-null ? Or in case they are two different queries, can you please provide both? – Daniele B Mar 01 '18 at 11:56
  • Firestore doesn't support 'isNotEqual' operator so you can't check for null value directly, but there is a workaround [explained here](https://stackoverflow.com/a/48481812/4218640). – ElectroBuddha Mar 04 '18 at 11:30
  • @ElectroBuddha that workaround is to detect "not null", by operating on a String domain. Unfortunately it doesn't solve the problem of detecting "null" – Daniele B Mar 05 '18 at 12:55
  • @DanieleB In that case check [this answer](https://stackoverflow.com/a/46813032/4218640). I'm not sure for Go language, but in Swift it also doesn't work when using `nil` value in 'where' statement, but it works if you instead use instance of null object `NSNull()`. Could you check if there is an equivalent representation of null object in Go lang? – ElectroBuddha Mar 06 '18 at 07:52
  • @FrankvanPuffelen is there a NSNull() equivalent for Go? – Daniele B Mar 06 '18 at 13:05

1 Answers1

1

Currently a bug in the Go client. Track at https://github.com/GoogleCloudPlatform/google-cloud-go/issues/922.