删除 Firestore 中数组的索引
Delete index at array in Firestore
我在文档中得到了这些数据:
我想删除索引 0。我该怎么做?这应该可以达到我的想法:
db.collection("data").document("free").updateData(["deleteme.deletemee.0" : FieldValue.delete()]) { (errr) in
print(errr)
}
但是 errr 打印了 nil,并没有删除任何内容。在获取文档时,我注意到使用此代码时数据有些奇怪:
db.collection("data").document("free").getDocument { (doc, err) in
guard let _doc = doc,
doc?.exists ?? false else{ return }
print(_doc.data())
}
打印出来:
["deleteme": {
deletemee = (
1 //this is the value for the key, but where is my key?! :(
);
}]
我看不到钥匙,它在哪里?如何删除 Firestore 中索引处的内容?谢谢
目前无法修改存储在 Cloud Firestore 中的数组的单个元素。
如果您将数据存储为地图(键无关紧要),如下所示:
{
name: "sam",
things: {
one: "value",
two: "value"
}
}
然后你可以像这样删除单个元素:
// Delete the things.one data
db.collection("whatever").document("whatever").updateData([
"things.one": FieldValue.delete(),
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
现在数据将如下所示:
{
name: "sam",
things: {
two: "value"
}
}
终于支持数组操作了。 通过值(不是索引) 现在支持删除、添加等操作:
目前,虽然 我遇到了一些错误,但目前有一些错误。
开发者博客here:
export const deleteArrayIndex = (collectionName, id, index) => {
db.collection(collectionName).doc(id).update(
{ [index]: firebase.firestore.FieldValue.delete() }
).then(function () {
console.log(index + " is deleted");
}).catch(function (error) {
console.error("Error removing document: ", error);
});
}
我在文档中得到了这些数据:
我想删除索引 0。我该怎么做?这应该可以达到我的想法:
db.collection("data").document("free").updateData(["deleteme.deletemee.0" : FieldValue.delete()]) { (errr) in
print(errr)
}
但是 errr 打印了 nil,并没有删除任何内容。在获取文档时,我注意到使用此代码时数据有些奇怪:
db.collection("data").document("free").getDocument { (doc, err) in
guard let _doc = doc,
doc?.exists ?? false else{ return }
print(_doc.data())
}
打印出来:
["deleteme": {
deletemee = (
1 //this is the value for the key, but where is my key?! :(
);
}]
我看不到钥匙,它在哪里?如何删除 Firestore 中索引处的内容?谢谢
目前无法修改存储在 Cloud Firestore 中的数组的单个元素。
如果您将数据存储为地图(键无关紧要),如下所示:
{
name: "sam",
things: {
one: "value",
two: "value"
}
}
然后你可以像这样删除单个元素:
// Delete the things.one data
db.collection("whatever").document("whatever").updateData([
"things.one": FieldValue.delete(),
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
现在数据将如下所示:
{
name: "sam",
things: {
two: "value"
}
}
终于支持数组操作了。 通过值(不是索引) 现在支持删除、添加等操作:
目前,虽然
开发者博客here:
export const deleteArrayIndex = (collectionName, id, index) => {
db.collection(collectionName).doc(id).update(
{ [index]: firebase.firestore.FieldValue.delete() }
).then(function () {
console.log(index + " is deleted");
}).catch(function (error) {
console.error("Error removing document: ", error);
});
}