添加/更新数组Firestore

时间:2019-05-11 23:18:20

标签: javascript angular google-cloud-firestore angularfire2

我正在与Firestore合作。以下代码使用单个元素更新矩阵,也就是说,每次用户执行该功能时,都会更新该字段:

updateFavoritos(key) {
    const user = firebase.auth().currentUser;
    this.afs.doc('eventos/' + key).update({
      favoritos: [ user.uid ],
    });
  }

这看起来像这样: enter image description here

但是我要添加而不是更新该矩阵,是这样的:

enter image description here

1 个答案:

答案 0 :(得分:0)

Firestore具有处理数组的方法。链接到docs

var washingtonRef = db.collection("cities").doc("DC");

// Atomically add a new region to the "regions" array field.
washingtonRef.update({
    regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
});

// Atomically remove a region from the "regions" array field.
washingtonRef.update({
    regions: firebase.firestore.FieldValue.arrayRemove("east_coast")
});