Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete index at array in Firestore

I got this data in my document:

enter image description here

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.

like image 443
J. Doe Avatar asked Oct 13 '17 22:10

J. Doe


People also ask

Is there any way to update a specific index from the array in firestore?

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.

How do I edit an array in firestore?

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.


1 Answers

Array operations have finally been supported. Deletion, addition, etc. are supported via the value (not the index) now:See this

At the moment, there are a few bugs at the moment though as this one I encountered.

The dev blog here:

like image 179
ranjjose Avatar answered Oct 10 '22 12:10

ranjjose