Delete index at array in Firestore

后端 未结 3 580
臣服心动
臣服心动 2020-11-30 14:15

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\").         


        
相关标签:
3条回答
  • 2020-11-30 14:22

    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"
      }
    }
    
    0 讨论(0)
  • 2020-11-30 14:24
    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 讨论(0)
  • 2020-11-30 14:42

    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:

    0 讨论(0)
提交回复
热议问题