Firestore 从集合中获取文档 ID
Firestore Getting documents id from collection
我正在尝试使用 ID 检索我的文档,但无法弄清楚。
目前我检索我的文档是这样的:
const racesCollection: AngularFirestoreCollection<Races> = this.afs.collection('races');
return racesCollection.valueChanges();
我确实得到了我的文档列表,但是它们没有文档 ID。
如何为每个文档检索它?
我终于找到了解决办法。 Victor 与文档数据关系密切。
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
在数据库中添加文档之前可以获取 ID:
var idBefore = this.afs.createId();
console.log(idBefore);
要获取集合中文档的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 };
});
});
doc.id
获取 UID。
像这样与一个对象的其余数据合并:
Object.assign({ uid: doc.id }, doc.data())
对于 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 };
});
})
);
对于 angular 8 和 Firebase 6,您可以使用选项 ID 字段
getAllDocs() {
const ref = this.db.collection('items');
return ref.valueChanges({idField: 'customIdName'});
}
这将在具有指定键 (customIdName) 的对象上添加文档的 ID
由于您使用的是 angularFire,因此如果您要返回默认的 firebase 方法来实现,则没有任何意义。
AngularFire 本身已经实现了适当的机制。只是必须使用它。
angularFire 的 valueChanges()
方法提供了一个重载,用于获取集合中每个文档的 ID,只需将对象作为 参数 添加到该方法即可。
valueChanges({ idField: 'id' })
这里'idField'必须和原来一样。 'id' 可以是您希望调用文档 ID 的任何名称。
那么返回数组中的每个文档对象将如下所示。
{
field1 = <field1 value>,
field2 = <field2 value>,
..
id = 'whatEverTheDocumentIdWas'
}
然后您可以通过引用您命名的字段轻松获取文档 ID。
AngularFire 5.2.0
对于文档参考而非集合,您需要:
// 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 };
});
as .valueChanges({ idField: 'id'});
在这里不起作用。我假设它没有实现,因为通常你通过 id 搜索文档...
我试过了
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;
});
})
);
我正在尝试使用 ID 检索我的文档,但无法弄清楚。
目前我检索我的文档是这样的:
const racesCollection: AngularFirestoreCollection<Races> = this.afs.collection('races');
return racesCollection.valueChanges();
我确实得到了我的文档列表,但是它们没有文档 ID。
如何为每个文档检索它?
我终于找到了解决办法。 Victor 与文档数据关系密切。
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
在数据库中添加文档之前可以获取 ID:
var idBefore = this.afs.createId();
console.log(idBefore);
要获取集合中文档的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 };
});
});
doc.id
获取 UID。
像这样与一个对象的其余数据合并:
Object.assign({ uid: doc.id }, doc.data())
对于 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 };
});
})
);
对于 angular 8 和 Firebase 6,您可以使用选项 ID 字段
getAllDocs() {
const ref = this.db.collection('items');
return ref.valueChanges({idField: 'customIdName'});
}
这将在具有指定键 (customIdName) 的对象上添加文档的 ID
由于您使用的是 angularFire,因此如果您要返回默认的 firebase 方法来实现,则没有任何意义。 AngularFire 本身已经实现了适当的机制。只是必须使用它。
angularFire 的valueChanges()
方法提供了一个重载,用于获取集合中每个文档的 ID,只需将对象作为 参数 添加到该方法即可。
valueChanges({ idField: 'id' })
这里'idField'必须和原来一样。 'id' 可以是您希望调用文档 ID 的任何名称。
那么返回数组中的每个文档对象将如下所示。
{
field1 = <field1 value>,
field2 = <field2 value>,
..
id = 'whatEverTheDocumentIdWas'
}
然后您可以通过引用您命名的字段轻松获取文档 ID。
AngularFire 5.2.0
对于文档参考而非集合,您需要:
// 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 };
});
as .valueChanges({ idField: 'id'});
在这里不起作用。我假设它没有实现,因为通常你通过 id 搜索文档...
我试过了
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;
});
})
);