【问题标题】:add / update array Firestore添加/更新数组 Firestore
【发布时间】:2019-05-11 23:18:20
【问题描述】:

我正在使用 Firestore。以下代码使用单个元素更新矩阵,即每次用户执行该函数时,都会更新该字段:

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

这看起来像这样:

但是我该怎么做才能添加而不是更新该矩阵,就是这样说:

【问题讨论】:

标签: javascript angular google-cloud-firestore angularfire2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 2020-05-05
    • 2019-05-08
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多