How to delete the array elements at index in firestore in javascript

后端 未结 3 1500
面向向阳花
面向向阳花 2021-01-26 14:54
    var arr = [ \"GKRLrcpSL2BmKx3BfvWj\",\"fMwH404fweoFJJqyE5Nf\" , \"DTurwbw64CWw4WosUQtC\" ] //
arr.forEach((element) => {
    console.log(element);
    db.collecti         


        
相关标签:
3条回答
  • 2021-01-26 15:17

    If that works for you, use a map instead of an array, putting dummy values if necessary. Then you can remove the field directly.

    fill: {
      a: xxx,
      b: xxx,
      check: xxx
    }
    
    db.collection("Check").doc(element).update({
      fill.check: firebase.firestore.FieldValue.delete()
    })
    
    0 讨论(0)
  • 2021-01-26 15:22

    My solution is based on specifying the item ID to remove, example:

     firebase
            .firestore()
            .collection('projects')    // Collection
            .doc(project.id)           // ID string
            .delete()                  // Promise
    
    0 讨论(0)
  • 2021-01-26 15:28

    FieldValue.arrayRemove() only works with the complete value of the array item to remove. It won't work for removing items by index. In fact, Firestore doesn't offer any way to directly modify array items by index.

    What you'll have to do instead is read the document, modify the "fill" array in memory as you see fit, then update the document with the new value back to the document.

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