且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firestore:如何更新文档的特定字段?

更新时间:2023-02-14 08:21:37

好,您必须执行以下步骤:

Ok you have to do the following steps:

  • 首先请确保您创建的查询名称或ID或什至两者都必须是唯一的
  • 然后您通过snapshotChanges订阅此查询
  • 接下来,您将从所查询的对象中获取ID
  • 此后,您将使用此ID使用新值更新文档

它看起来像这样:

updateDoc(_id: string, _value: string) {
  let doc = this.afs.collection('options', ref => ref.where('id', '==', _id));
  doc.snapshotChanges().pipe(
    map(actions => actions.map(a => {                                                      
      const data = a.payload.doc.data();
      const id = a.payload.doc.id;
      return { id, ...data };
    }))).subscribe((_doc: any) => {
     let id = _doc[0].payload.doc.id; //first result of query [0]
     this.afs.doc(`options/${id}`).update({rating: _value});
    })
}