如何删除 Firestore 文档中的字段?
How to delete a field in a Firestore Document?
如何删除 Cloud Firestore 中的文档字段? ...我正在使用下面的代码,但我不能。
this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`).update({
[currentUserId]: firebase.firestore.FieldValue.delete()})
这可能吗?如果可能,怎么做?
您可以尝试如下图:
// 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()
});
For some reason the selected answer (firebase.firestore.FieldValue.delete()
) did not work for me. but this did:
只需将该字段设置为 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
});
这对我有用。 (也用于删除具有空值的字段)
document.ref.update({
FieldToDelete: admin.firestore.FieldValue.delete()
})
使用 Firebase 版本 9(2022 年 2 月更新):
如果集合 “users” 具有 一个文档 (dWE72sOcV1CRuA0ngRt5),其字段为 “name” 、“年龄”和“性别”如下图:
users > dWE72sOcV1CRuA0ngRt5 > name: "John",
age: 21,
sex: "Male"
您可以使用以下代码删除字段 “年龄”:
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"
您可以使用以下代码删除多个字段 "age" 和 "sex":
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"
谨慎使用此 admin.firestore.FieldValue.delete()
因为如果您尝试删除文档中不可用的字段,查询将不起作用。
所以,我觉得设置null
比较好
this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`).update({
[currentUserId]: null})
或
await db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`)
.set({[currentUserId]: null}, { merge: true })
如何删除 Cloud Firestore 中的文档字段? ...我正在使用下面的代码,但我不能。
this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`).update({
[currentUserId]: firebase.firestore.FieldValue.delete()})
这可能吗?如果可能,怎么做?
您可以尝试如下图:
// 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()
});
For some reason the selected answer (
firebase.firestore.FieldValue.delete()
) did not work for me. but this did:
只需将该字段设置为 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
});
这对我有用。 (也用于删除具有空值的字段)
document.ref.update({
FieldToDelete: admin.firestore.FieldValue.delete()
})
使用 Firebase 版本 9(2022 年 2 月更新):
如果集合 “users” 具有 一个文档 (dWE72sOcV1CRuA0ngRt5),其字段为 “name” 、“年龄”和“性别”如下图:
users > dWE72sOcV1CRuA0ngRt5 > name: "John",
age: 21,
sex: "Male"
您可以使用以下代码删除字段 “年龄”:
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"
您可以使用以下代码删除多个字段 "age" 和 "sex":
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"
谨慎使用此 admin.firestore.FieldValue.delete()
因为如果您尝试删除文档中不可用的字段,查询将不起作用。
所以,我觉得设置null
this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`).update({
[currentUserId]: null})
或
await db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`)
.set({[currentUserId]: null}, { merge: true })