Delete Field in Firestore Document

2020-02-28 03:54发布

问题:

how to delete a Document Field in Cloud Firestore? ... I'm using the code below but I can not.

this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`).update({ 
[currentUserId]: firebase.firestore.FieldValue.delete()})

Anyone know how to do it?

回答1:

You can try as shown below:

//get the reference to the doc
let docRef=this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`);

// Remove the 'currentUserId' field from the document
let removeCurrentUserId = docRef.update({
    currentUserId: firebase.firestore.FieldValue.delete()
});


回答2:

Another solution that comes to my mind is, instead of focusing on updating the document deleting the field, update the entire document. Such as:

let data: Partial<ObjectType> = {
    //some fields here,
    thatField: '',
    //some fields there
};
this.angularFirestore.collection('collection_name').doc<ObjectType>('id').set(data);

Also, instead of initializing that field you may simply not include it.