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
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
The following will work for the web:
firebase.firestore().collection('abcd').doc("efgh").set({
name: "...",
nullField: null
})
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.