首页 > 解决方案 > Firestore - 如何从值映射中添加/减去

问题描述

我正在遵循 Firestore 存储数组的说明: https ://firebase.google.com/docs/firestore/solutions/arrays

现在我想做的是推送到这张地图。例如现在我有:

Contacts
   contact1: true

但我想添加或删除联系人,例如:

Contacts
   contact1: true
   contact2: true

我已经尝试获取联系人地图并使用推送方法,但我认为这不会起作用,因为它不是传统的数组。例如:

this.afs
  .doc(`groups/${group.id}`)
  .ref.get()
  .then(doc => {
    let contacts: Array<any> = doc.data().contacts;

    contacts.push({ // error here as push is not a function
      [contactId]: true
    });

    console.log(contacts);
  });

有没有更简单的方法来做到这一点 - 我错过了什么吗?

标签: angularfirebasegoogle-cloud-firestoreangularfire2

解决方案


只需推入地图

使用update()如下

const db = firebase.firestore();
const collection = db.collection('collectionId');

collection.doc(`groups/${group.id}`).update({

      "Contacts.contact3":true

    }).then(function(){

       console.log("Successfully updated!");

});

推荐阅读