【发布时间】:2018-11-13 14:51:43
【问题描述】:
我有一个简单的逻辑,将 2 个 Firestore id 与 _ 组合在一起,即 id1_id2。在检索复合键时,我使用 _ 将其拆分。因此,Firestore的自动生成的ID是否可能包含_下划线,从而破坏了我用_下划线分割的简单逻辑?
我通过 AngularFire2 使用 Firestore 和 Angular。
一个简化的 sn-p。
constructor(private afs: AngularFirestore) {
const id1 = this.afs.createId();
const id2 = this.afs.createId();
const compositeKey = id1 + '_' + id2;
}
我需要compositeKey,因为我想通过Google Developer Expert 实现an example in here:示例relationships/{followerID_followedID}。还有其他可能的方法来实现相同的结果。但是,在遵循使用 CompositeKey 进行教学的教程时,我想了解使用 _ 下划线分割 2 个 id 是否没有冲突?
compositeKey 不用于 Firestore 中的直接查询。它用于 url 路由。我有一个路由呼叫localhost:4200/relationship/follwerId_followId
我会在 Angular 中读取 relationship/:id,然后用 _ 下划线分隔,最后查询 .collection('user_profile').doc(followerId) 和 .collection('user_profile').doc(followedId) 以在单个页面中显示两个用户的个人资料。
【问题讨论】:
-
请编辑您的问题以包含一些代码,这些代码显示您如何生成 ID,以及您正在做什么需要像这样连接它们。可能有更好的方法来完成您的工作,而不依赖于 Firestore 的实施细节。
-
@DougStevenson 感谢您回复我的询问。刚刚添加了一个简化的 sn-p。
-
是的,我知道您正在连接两个 ID。我的问题是为什么你觉得你需要这样做。您是否在查询中使用它们?这样做的目的是什么?
-
不在 Firestore 查询中使用它们。使用它们来操作 url 路由。这意味着我有一个具有 url localhost:4200/relationship/followerId_followedId 的应用程序,它将显示关注者和关注的所有信息。
-
不要对它们进行分隔。直接连接它们即可。
标签: firebase google-cloud-firestore