A. Cedano
A. Cedano

Reputation: 965

Null vs empty String in Firestore, any difference?

There is a difference between one field of type null and one field of type string that is empty?

In the documentation it says:

Null........... 1 byte

String....... total of bytes with UTF-8 encode + 1

In this case, one empty string is equal to a null value?

I'm creating my database and I'm trying to avoid empty data in some cases. I don't know if it's best to declare one field as null when it does not have data.

Upvotes: 1

Views: 3050

Answers (2)

Tomas
Tomas

Reputation: 1

When you get an empty string is equal to ""

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317692

A Firestore null is a completely different data type than a Firestore string. A null is never equal to any string. An empty string and a null might occupy the same amount of space, but that's where the similarities end.

You can even see from the documentation on value type ordering that they sort differently, where null always sorts "less than" any string in a query that involves different data types.

One of use of null as a document field is to make that field searchable without any actual data to index. If a document doesn't have a field with a value, that document will not be searchable by that field. As soon as you put a null in a field, you can query that field for equality with null.

Upvotes: 5

Related Questions