2

I use a collection of documents that I use to identify user groups. My intention is to only fill in the document id field with the user id without having other useless fields in the documents. But after doing some research, apparently, it's not possible to have empty documents.

So, my question is how to set a (dummy) field in a document to null, which is supposedly supported by Firestore according to the documentation. I'm working on this in both Android and Web, but I suppose code for any platform is OK.

Update: I have confirmed that simply putting null as the field in Web works just fine, however, when I try the equivalent in Android like this:

Map<String, Object> emptyData = new HashMap<>();
emptyData.put("nullField", null);

Android Studio warns me:

Passing 'null' argument to parameter annotated as @NotNull

Should I keep passing null or is there something else I should do?

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Jack
  • 5,354
  • 2
  • 29
  • 54

3 Answers3

1

The following will work for the web:

        firebase.firestore().collection('abcd').doc("efgh").set({
            name: "...",
            nullField: null
        })
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • 1
    This works for the web, however, when I try the equivalent in Android, it will warn me that the field is annotated with @NonNull, any ideas? – Jack Jan 07 '19 at 09:06
1

A solution that worked for me, as simple as it seems is setting the field you want to nullify to an empty string for strings and null for integers. No fancy stuff, if you want you can test it out by manually setting the values to null in your firebase firestore document browser.

this.itemDoc.update({item: ''});

or

this.itemDoc.update({item: null});

This was done using angularfire2, here's the link to the documentation : https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md

Moez ‌
  • 61
  • 5
  • You can try to ignore the warning caused by the annotation for now. I thought it could be from the "Object" type in the Map, I tried it on Netbeans, and it doesn't show me a warning. Keep up me updated on where you got please – Moez ‌ Jan 07 '19 at 09:26
1

Assuming that the the userId property is of type String, please use the following code in order to update all users with the userId = null:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
usersRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            List<String> list = new ArrayList<>();
            for (DocumentSnapshot document : task.getResult()) {
                list.add(document.getId());
            }

            for (String id : list) {
                rootRef.collection("Users").document(id).update("userId", null).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "Username updated!");
                    }
                });
            }
        }
    }
});

If you are using a model class for your user, please see my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    This works if it's updating the field, however, if I'm trying to set the document (create or update), it will warn me that the field that's supposed to be null is annotated with @NotNull, any ideas? – Jack Jan 07 '19 at 09:08
  • That's the corect behaviour if you added to your fields this annotation `@NonNull`. So if you want to hold `null` values, you shouldn't assure the compiler that the fields won't be `null`. – Alex Mamo Jan 07 '19 at 09:10
  • but I didn't annotate the field as not null, please see my update. – Jack Jan 07 '19 at 09:12
  • Check [this](https://stackoverflow.com/questions/29497755/intellij-idea-complains-about-null-check-for-notnull-parameter) and [this](https://stackoverflow.com/questions/34094039/using-notnull-annotation-in-method-argument) out. – Alex Mamo Jan 07 '19 at 09:14
  • So basically, I can just suppress the warning, right? – Jack Jan 07 '19 at 09:17
  • Give it a try and see the behaviour. – Alex Mamo Jan 07 '19 at 09:18
  • It is a workaround but is it acceptable to set it to false instead? – Xiiryo Sep 28 '20 at 12:24
  • @Xiiryo You can set/update it according to your needs. – Alex Mamo Sep 28 '20 at 14:24