Firestore从集合中获取文档ID

时间:2017-10-24 00:29:55

标签: angular firebase nosql google-cloud-firestore

我试图用id检索我的文件,但无法弄明白。
目前我检索这样的文件:

const racesCollection: AngularFirestoreCollection<Races> = this.afs.collection('races');
return racesCollection.valueChanges();

我确实完整地获得了我的文档列表,但是没有文档ID。

如何为每个文档检索它?

10 个答案:

答案 0 :(得分:26)

要获取集合中文档的ID,您必须使用snapshotChanges()

this.shirtCollection = afs.collection<Shirt>('shirts');
// .snapshotChanges() returns a DocumentChangeAction[], which contains
// a lot of information about "what happened" with each change. If you want to
// get the data and the id use the map operator.
this.shirts = this.shirtCollection.snapshotChanges().map(actions => {
  return actions.map(a => {
    const data = a.payload.doc.data() as Shirt;
    const id = a.payload.doc.id;
    return { id, ...data };
  });
});

文档 https://github.com/angular/angularfire2/blob/7eb3e51022c7381dfc94ffb9e12555065f060639/docs/firestore/collections.md#example

答案 1 :(得分:23)

我终于找到了解决方案。维克多与博士数据很接近。

const racesCollection: AngularFirestoreCollection<Race>;
return racesCollection.snapshotChanges().map(actions => {       
  return actions.map(a => {
    const data = a.payload.doc.data() as Race;
    data.id = a.payload.doc.id;
    return data;
  });
});

ValueChanges()不包含元数据,因此我们必须在需要文档ID时使用SnapshotChanges(),然后按照此处所述正确映射https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md

答案 2 :(得分:23)

对于angular6 +

this.shirtCollection = afs.collection<Shirt>('shirts');
this.shirts = this.shirtCollection.snapshotChanges().pipe(
    map(actions => {
    return actions.map(a => {
        const data = a.payload.doc.data() as Shirt;
        const id = a.payload.doc.id;
        return { id, ...data };
    });
    })
);

答案 3 :(得分:11)

由于您使用的是angularFire,因此如果要返回默认的Firebase方法进行实现则没有任何意义。 AngularFire本身已实现了适当的机制。只需使用它即可。

通过简单地将对象作为 parameter 添加到方法中,angularFire的

valueChanges()方法提供了重载,以获取集合中每个文档的ID。

valueChanges({ idField: 'id' })

“ idField”必须与原样相同。 “ id”可以是您希望调用文档ID的任何内容。

然后,返回数组上的每个文档对象将如下所示。

{
  field1 = <field1 value>,
  field2 = <field2 value>,
  ..
  id = 'whatEverTheDocumentIdWas'
}

然后,您可以通过引用您命名的字段来轻松获取文档ID。

AngularFire 5.2.0

答案 4 :(得分:6)

doc.id获取UID。

结合一个对象的其余数据,如下所示:

Object.assign({ uid: doc.id }, doc.data())

答案 5 :(得分:3)

对于文档参考而非收藏,您需要:

// when you know the 'id'

this.afs.doc(`items/${id}`)
  .snapshotChanges().pipe(
    map((doc: any) => {
      const data = doc.payload.data();
      const id = doc.payload.id;
      return { id, ...data };
    });

.valueChanges({ idField: 'id'});的功能在这里不起作用。我认为它没有实现,因为通常您通过id搜索文档...

答案 6 :(得分:2)

在数据库中添加文档之前可以获取ID:

var idBefore =  this.afs.createId();
console.log(idBefore);

答案 7 :(得分:2)

对于angular 8和Firebase 6,您可以使用选项ID字段

      getAllDocs() {
           const ref = this.db.collection('items');
           return ref.valueChanges({idField: 'customIdName'});
      }

这会使用指定的键(customIdName)在对象上添加文档的ID

答案 8 :(得分:0)

尝试过!

colWithIds$<T>(ref: CollectionPredicate<T>, queryFn?): Observable<any[]> {
    return this.col(ref, queryFn).snapshotChanges().pipe(
    map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        return { id, ...data };
      });
    }));
  }

但是我遇到了这个错误

[ts]传播类型只能从对象类型创建。 const数据:T

答案 9 :(得分:0)

我已经尝试过

return this.db.collection('items').snapshotChanges().pipe(
          map(actions => {       
            return actions.map(a => {
              const data = a.payload.doc.data() as Item;
              data.id = a.payload.doc.id;
              data.$key = a.payload.doc.id;
              return data;
            });
          })
        );
相关问题