简体   繁体   English

如何删除 Firestore 文档中的字段?

[英]How to delete a field in a Firestore Document?

How to delete a Document Field in Cloud Firestore?如何删除 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()})

Is it possible, and if so, how?这可能吗?如果可能的话,怎么办?

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()
});

This worked for me.这对我有用。 (also worked to delete field with null value) (也用于删除具有空值的字段)

document.ref.update({
  FieldToDelete: admin.firestore.FieldValue.delete()
})

With Firebase Version 9 (Feb, 2022 Update):使用 Firebase 版本 9(2022 年 2 月更新):

If there is the collection "users" having one document(dWE72sOcV1CRuA0ngRt5) with the fields "name" , "age" and "sex" as shown below:如果集合“users”一个文档(dWE72sOcV1CRuA0ngRt5) ,其中包含字段“name”“age”“sex” ,如下所示:

users > dWE72sOcV1CRuA0ngRt5 > name: "John", 
                               age: 21, 
                               sex: "Male"

You can delete the field "age" with this code below:您可以使用以下代码删除“年龄”字段:

import { doc, updateDoc, deleteField } from "firebase/firestore";

const userRef = doc(db, "users/dWE72sOcV1CRuA0ngRt5");

// Remove "age" field from the document
await updateDoc(userRef, {
  "age": deleteField()
});
users > dWE72sOcV1CRuA0ngRt5 > name: "John",  
                               sex: "Male"

You can delete multiple fields "age" and "sex" with this code below:您可以使用以下代码删除多个字段“年龄”“性别”

import { doc, updateDoc, deleteField } from "firebase/firestore";

const userRef = doc(db, "users/dWE72sOcV1CRuA0ngRt5");

// Remove "age" and "sex" fields from the document
await updateDoc(userRef, {
  "age": deleteField(),
  "sex": deleteField()
});
users > dWE72sOcV1CRuA0ngRt5 > name: "John"

For some reason the selected answer ( firebase.firestore.FieldValue.delete() ) did not work for me.由于某种原因,所选答案( firebase.firestore.FieldValue.delete() )对我不起作用。 but this did:但这确实:

Simply set that field to null and it will be deleted!只需将该字段设置为null ,它将被删除!

// 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]: null
});

carefully using this admin.firestore.FieldValue.delete() Because query doesn't work if you try to delete not available field on the document.小心使用这个admin.firestore.FieldValue.delete()因为如果您尝试删除文档上不可用的字段,查询将不起作用。

So, I think it's better to set null所以,我认为最好设置为null

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

OR或者

await db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`)
            .set({[currentUserId]: null}, { merge: true })

IN CASE THE ABOVE ALL DIDNT WORKED FOR YOU (Like me) use this function如果以上所有内容都对您有用(像我一样),请使用此 function

const deleteField = async() => {
    firestore.collection("users").get().then(function(querySnapshot) {
      querySnapshot.forEach(function(doc) {
          doc.ref.update({
              date_of_birth: firestore.FieldValue.delete()
          });
      });
  });
}

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.此外,您可以简单地不包括它,而不是初始化该字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM