14

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.

3 Answers 3

32

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:

Sign up to request clarification or add additional context in comments.

3 Comments

This MUST be the answer now.
@Sampath yea times have changed, I made this answer now the accepted answer
In case if values are double, then arrayRemove needs to match the value up to last precision : (
5

It is not currently possible to modify individual elements of an array stored in Cloud Firestore.

If you stored the data as a map (the keys dont matter) like this:

{
  name: "sam",
  things: {
    one: "value",
    two: "value"
  }
}

Then you can delete individual elements like this:

// 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")
    }
}

Now the data will look like this:

{
  name: "sam",
  things: {
    two: "value"
  }
}

2 Comments

>:( hmm ok so arrays are pretty useless at this time since they can not be changed...
Yes right now they're not terribly useful, but these are just Beta limitations and in the future we will have better support for arrays / array queries.
-1
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);
    });
}

1 Comment

Hi Danish. Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. How to Answer. Kind Regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.