How I can manage optional values in Firebase Firestore and avoid empty fields in database?

firestore map vs array
firestore reference type
firestore enum

In my context, there is the possibility of optional values.

For example, in this database, the field iVisperas can be empty in multiple cases. I think is a bad practice to keep empty fields (?).

Into my code, I map the snapshot to one class.

I don't want to keep the field iVisperas as an empty field in Firestore but I don't know how to determine from the model class if this field exists or not.

How I can achieve this. This is my model class from the document. Actually, at getTituloVisperas method I check if field iVisperas is empty. If I want to create iVisperas field only when it contains data, how I can manage that into the model class?

public class MetaLiturgia {
    private String fecha;
    private int tiempo;
    private String semana;
    private String mensaje;
    private String salterio;
    private int color;
    private String meta;
    private String titulo;
    private String iVisperas;

    private int weekDay;

    public MetaLiturgia() {
    }

    public String getTituloVisperas() {
        if (iVisperas.equals("")){
            return titulo;
        }else{
            return iVisperas;
        }

    }
//...
}

For example, in this database the field iVisperas can be empty in multiple cases.

Seeing that all the fields in your MetaLiturgia class are set to private, I assume that you are using a constructor and public getters. If you want that your iVisperas property not to hold any value then simply do not instantiate it in the constructor or create another constructor without it.

If I want to create iVisperas field only when it contains data, how I can manage that into the model class?

In that case, you should use a Map and update that document only when the value for your iVisperas property is available.

I think is a bad practice to keep empty fields (?).

There is no benefit in keeping empty fields as long as are not needed. Remember, every property count in that 1 MiB (1,048,576 bytes) maximum size for a document.

According to Suhas Ch's answer, please also note that a property of type String which is empty ("") and a property which holds null, occupy the same amount of space in a document.

Edit:

Another possible solution is to assign a default value, in this case, 0, right inside the model (MetaLiturgia) class.

Perform simple and compound queries in Cloud Firestore, Query operators. The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be� Hopefully this comparison has helped you settle on a Firebase database solution. Now you can learn how to add a database to your Firebase projects. For Cloud Firestore, take a look at Get started with Cloud Firestore. For Realtime Database, read the Get Started topic for your platform: iOS, Android, C++, Unity, Web, or REST.

While creating field, set the type for iVisperas as null

and in the model class

public class MetaLiturgia {
    private String fecha;
    private int tiempo;
    private String semana;
    private String mensaje;
    private String salterio;
    private int color;
    private String meta;
    private String titulo;
    private String iVisperas;

    private int weekDay;

    public MetaLiturgia() {
    }

    public String getTituloVisperas() {
        if (iVisperas.equals(null)){
            return titulo;
        }else{
            return iVisperas;
        }

    }
//...
}

Firebase Database Security Rules API, Then, if /users/barney/active contained the value true, a user authenticated with a uid of "barney" could write to the /comments node. data. A� Important: Unlike "push IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do not provide any automatic ordering. If you want to be able to order your documents by creation date, you should store a timestamp as a field in the documents.

When do you know if the field is empty? You could delete attribute from your model class and create another class which inherits fro your model class. There you can add the value. When it comes to saving the object, you wont write the field if its not used. But I dont know if this is worth the effort, since you will have to create a newObject of the subclass once your attribute is used.

Cloud Firestore: On data constraints and evolvability, Cloud Firestore is Google's highly scalable, NoSQL database for numeric value, and a name field with a non-empty string value. values to the population field without violating the schema constraint. There are many helpful references on the subject of Firestore data modeling from the Firebase team. Cloud Firestore Security Rules allow you to control access to documents and collections in your database. The flexible rules syntax allows you to create rules that match anything, from all writes to the entire database to operations on a specific document.

Firebase Cloud Firestore — Add, Update, Get, Delete, OrderBy, Limit , First, get a reference to the Firestore database using firestore() method and store it true} as a second argument to the set() method which will prevent overwriting an with new values to the targetted document if the name and age fields exist. cursor methods so that users can have more control on the data consumption. implementation 'com.google.firebase:firebase-database-ktx:19.3.1' Configure Realtime Database Rules The Realtime Database provides a declarative rules language that allows you to define how your data should be structured, how it should be indexed, and when your data can be read from and written to.

Get started: write, test, and deploy your first functions, (Optional) If you are creating a new project, you can edit the Project ID. When you initialize Firebase SDK for Cloud Functions, you create an empty project passes a text value to the HTTP endpoint and inserts it into the database under the Setting an 'uppercase' field in Cloud Firestore document returns a Promise. in and array-contains-any support up to 10 comparison values. You can use only one in or array-contains-any clause per query. You can't use both in and array-contains-any in the same query. You can combine array-contains with in but not with array-contains-any. You cannot order your query by any field included in an equality (=) or in clause

Security Rules, Firestore and Firebase Storage both use Firebase's new security rules syntax, while the original Firebase Realtime Database uses the original JSON security rules syntax The document-name fields can be set to wildcard values as well, which looks like this It's purely optional, but it might be easier to read in some cases. in and array-contains-any support up to 10 comparison values. You can use only one in or array-contains-any clause per query. You can't use both in and array-contains-any in the same query. You can combine array-contains with in but not with array-contains-any. You cannot order your query by any field included in an equality (=) or in clause

Comments
  • Ok. As you says, i'm usging public setters and getters in the class. And I using a Map for fill the class: JsonElement jsonElement = gson.toJsonTree(calSnapshot.get("meta")); mLiturgia = new Liturgia(); mLiturgia.setMetaLiturgia(gson.fromJson(jsonElement, MetaLiturgia.class));
  • No, you should use a Map to update the value of iVisperas property in the document, only when the data is available. If you don't have any data, don't store an empty ("") String, not even a null. So add that property only when you have some data to store in.
  • OK. But how i determine from the class (model) when there is data for iVisperas property. This is my issue at this moment. How I can achieve this in the class?
  • When you are mapping a document to an object of your MetaLiturgia class, simply check the iVisperas field for nullity. If that field is == to null it means that it doesn't exist, otherwise it has some value. But you may simply want to use toObject(MetaLiturgia.class) method rather than other methods.
  • For my context i solved the issue creating the field with a default value (0) in the model and then i check if after to map the snapshoot to the class the value of this field is 0 or not. I use the content of meta for multiple activities in my app and in some cases i need to change date based on the value of iVisperas. I think i found my way thanks to your comments and suggestions.
  • Is not a bad pratice to maintain null or empty values in Firebase?
  • empty values? - Maybe. null? - I think no