question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FirestoreProperty: allow ignoring null values

See original GitHub issue

I would like to prevent serializing null values when using DocumentReference.SetAsync to keep documents cleaner and to save storage space. Something like this would be useful:

[FirestoreProperty(nullValueHandling: NullValueHandling.Ignore)]
public string Name { get; set; }

Is there any way to do this currently?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
kinexcommented, Nov 12, 2019

OK. For possible comments from other developers I will present here one real-world sample use case where I have found it useful not to write null values. See this data model (simplified):

public enum EventType { Refuel, Expense, Service, Trip };

class Refuel { } // refuel event details
class Expense { } // expense event details
class Service { } // service event details
class Trip { } // trip details

// common data for all event types and container for event type specific details
class Event
{
    public string Id { get; set; }
    public DateTimeOffset DateTime { get; set; }
    public string Notes { get; set; }

    public EventType EventType { get; set; }

    public Refuel Refuel { get; set; }
    public Expense Expense { get; set; }
    public Service Service { get; set; }
    public Trip Trip { get; set; }
}

For a comparison here is the same class defined in Flutter/Dart where I can omit the null values easily as I want:

@JsonSerializable()
class Event
{
  String id;
  DateTime dateTime;
  String notes;

  EventType eventType;

  @JsonKey(includeIfNull: false)
  Refuel refuel;

  @JsonKey(includeIfNull: false)
  Expense expense;

  @JsonKey(includeIfNull: false)
  Service service;

  @JsonKey(includeIfNull: false)
  Trip trip;
}

Event contains common data to all events. Depending on Event.EventType only one of the Refuel/Expense/Service/Trip contains event specific data (others must be null).

So when I am looking at those documents for example in Firebase console while debugging my app, I wouldn’t like to even see those null values, because they just make reading the documents harder. Also, I don’t know how much those null values take space, but if and when my app has millions of users those unnecessary null values may start to cost me.

Someone could say I should move Refuel/Expense/Service and Trip to a different collection and only link them with an id field. I did consider that also but I like this current schema. it is easy to use and also cost effective.

1reaction
jskeetcommented, May 5, 2022

@MilosGrgov90: I haven’t heard anything from the Firestore team about this in the last 2 1/2 years, no. I’ll ping them to see if there has been any activity around this in other languages.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to exclude a property only if it's null in Firestore?
Is there any possibility to exclude the name property only if it's null? If I pass a real value I want the object...
Read more >
Data model
All public instance properties with the FirestoreProperty attribute ... The converter will never be called with a null value from Google.
Read more >
Untitled
Ignore undefined properties firestore Reference property from Firestore is always undefined Handling undefined Values with Firebase - Stack Overflow ...
Read more >
How can I set firestore to ignore undefined values?
Some of my object fields have undefined values which firestore doesn't accept. I want firestore to ignore these fields and store the rest...
Read more >
CRUD With Blazor Using Google Cloud Firestore
This will allow us to map this class object to Firestore collection. Only those class properties, which are marked with [FirestoreProperty] ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found