简体   繁体   English

Angularfire2 firestore-文档“字符串斜杠”表示法

[英]Angularfire2 firestore - document “string slash” notation

Information around referencing documents with this "string slash" notation is a little sparse. 有关使用这种“字符串斜杠”表示法引用文档的信息很少。 Here is an example provided in the docs : 这是docs中提供的示例:

constructor(private afs: AngularFirestore) {
    this.userDoc = afs.doc<Item>('user/david');
    ...
}

I can tell it's referencing the user collection and getting a doc with a unique id david . 我可以说这是在引用user集合并获取具有唯一id david的文档。 But how can I determine the field for the unique index (in this case david ) that firestore looks up in that string-slash notation? 但是,如何确定firestore在该字符串斜杠符号中查找的唯一索引(在本例中为david )的字段?

David here! 大卫在这里!

1 + 2) Think of david as the primary key in the users collection. 1 + 2)将david视为users集合中的主键。

3) You use a generated ID when you can ID is not important and you can get back the document with a query. 3)当ID并不重要并且可以通过查询取回文档时,请使用生成的ID。

constructor(private afs: AngularFirestore): {
  const shirtsCollection = afs.collection<Item>('tshirts', ref => {
    return ref.where('price', '==', 10.00);
  });
  this.shirtsUnder10$ = shirtsCollection.valueChanges();
}

In some less common cases you an also create a lookup collection. 在一些不太常见的情况下,您还会创建一个查找集合。 Where you have one known id that contains a list of generated ids. 您拥有一个已知ID的列表,其中包含生成的ID的列表。 This is useful for situations like "event attendees". 这对于“活动参加者”等情况很有用。 If you know the ID of the event, you can get back a list of attendees IDs for the users who attended the event. 如果您知道活动的ID,则可以获取参加活动的用户的与会者ID列表。

I didn't realize that the ID is implicitly set when you use doc() and set() in this way. 当您以这种方式使用doc()set()时,我没有意识到ID是隐式设置的。 In this case, david was supplied as the id: 在这种情况下,提供了david作为id:

this.afs.doc(`user/david`).set(userObject);

So once you add it like that, it can be recalled by the ID you set. 因此,一旦添加了它,就可以通过您设置的ID对其进行调用。 Calling that method over will replace (or "destructively set", as the docs note) what is stored under that ID. 调用该方法将替换(或“破坏性地设置”,如文档所述)该ID下存储的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Angularfire2和Firestore自动完成 - Autocomplete with Angularfire2 and Firestore 如何在angularfire2 v5中使用Firestore检索生成的文档ID - How to retrieve a generated document id with firestore in angularfire2 v5 如何在 AngularFire2 的 Firestore 中检索集合的文档 ID - How to retrieve document id's for collections in AngularFire2's Firestore Firestore,Angular,AngularFire2-添加带有引用类型字段的文档 - Firestore, Angular, AngularFire2 - Adding document with a reference type field 带有Firestore的Angularfire2:从文档中删除特定字段 - Angularfire2 with firestore: delete specific fields from a document 使用 Angularfire2 和 firestore 加入查询 - Join query with Angularfire2 and firestore Angularfire2,Firestore文档检索问题(使用.get,可能与ngOnChanges相关) - Angularfire2, Firestore Document Retrieval Issue (using .get, possibly related to ngOnChanges) 使用AngularFire2 / Firestore在Ionic / Angular中的模板中从文档字段中查找引用 - Looking up a reference from a document field from the template in Ionic/Angular with AngularFire2/Firestore flamefire2事务和firestore中的批处理写入 - angularfire2 transactions and batch writes in firestore Angularfire2 Firestore-将可观察到的内容展开为JSON数组 - Angularfire2 Firestore - Unwrap Observable into JSON Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM