I got this data in my document:
I want to delete index 0. How do I do this? This should do the trick I thought:
db.collection("data").document("free").updateData(["deleteme.deletemee.0" : FieldValue.delete()]) { (errr) in
print(errr)
}
But the errr prints nil, and nothing is removed. When getting the document I noticed something strange about the data when using this code:
db.collection("data").document("free").getDocument { (doc, err) in
guard let _doc = doc,
doc?.exists ?? false else{ return }
print(_doc.data())
}
This prints out:
["deleteme": {
deletemee = (
1 //this is the value for the key, but where is my key?! :(
);
}]
I cannot see the key, where is it? How to delete something at an index in Firestore? Thanks.
Is there any way to update a specific index from the array in Firestore? No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.
When it comes to the official documentation regarding how to update elements in an array in Firestore, it says that: If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present.
Array operations have finally been supported. Deletion, addition, etc. are supported via the value (not the index) now:
At the moment, there are a few bugs at the moment though as this one I encountered.
The dev blog here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With