我正在与Firestore合作。以下代码使用单个元素更新矩阵,也就是说,每次用户执行该功能时,都会更新该字段:
updateFavoritos(key) {
const user = firebase.auth().currentUser;
this.afs.doc('eventos/' + key).update({
favoritos: [ user.uid ],
});
}
但是我要添加而不是更新该矩阵,是这样的:
答案 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")
});