Angular 如何在单独的firestore集合上进行内部联接?
我目前正在尝试建立一个网站,用户可以使用4个单独的过滤器(例如类型、位置等)指定他们希望看到的数据。我最初在尝试查询我的firebase时遇到了使用多个“in”或“array contains any”运算符的问题,因为我只允许使用其中一个运算符一次(例如,我无法在x中执行where type,在y中执行where location,等等) 因此,我的想法是将这些查询分开,让每个过滤器单独查询firebase,并将所有这些集合内部连接到一个具有正确数据的集合(例如,从位置y到要求z,具有所有类型为x的机会的集合)。然而,不幸的是,我不知道如何将这3个不同的集合进行内部连接。如果可能的话,我将不胜感激Angular 如何在单独的firestore集合上进行内部联接?,angular,firebase,google-cloud-firestore,angularfire,Angular,Firebase,Google Cloud Firestore,Angularfire,我目前正在尝试建立一个网站,用户可以使用4个单独的过滤器(例如类型、位置等)指定他们希望看到的数据。我最初在尝试查询我的firebase时遇到了使用多个“in”或“array contains any”运算符的问题,因为我只允许使用其中一个运算符一次(例如,我无法在x中执行where type,在y中执行where location,等等) 因此,我的想法是将这些查询分开,让每个过滤器单独查询firebase,并将所有这些集合内部连接到一个具有正确数据的集合(例如,从位置y到要求z,具有所有类型
// Step 1: Filter by the type of the opportunity
if (isFilterType){
this.typeColl = this.afs.collection<Opportunity>('opportunities', ref => ref.where('oppoType','in',oppoType))
}
// Step 2: Filter by the location of the opportunity itself
if (isFilterLocation){
this.locationColl = this.afs.collection<Opportunity>('opportunities', ref => ref.where('location','in',location))
}
// Step 3: Filter by the requirements that the opportunity has
if (isFilterRequirements){
this.requirementsColl = this.afs.collection<Opportunity>('opportunities', ref => ref.where('requirements','array-contains-any',requirements))
}
//步骤1:按商机类型筛选
如果(isFilterType){
this.typeColl=this.afs.collection('opportunities',ref=>ref.where('oppoType','in',oppoType))
}
//步骤2:按商机本身的位置筛选
if(iFilterLocation){
this.locationColl=this.afs.collection('opportunities',ref=>ref.where('location','in',location))
}
//步骤3:根据opportunity具有的需求进行筛选
if(iFilterRequirements){
this.requirementsColl=this.afs.collection('opportunities',ref=>ref.where('requirements','array-contains-any',requirements))
}
Firestore没有内部联接类型查询。您必须在客户机上编写代码,以便根据需要合并所有这些查询的结果。这意味着您必须阅读所有查询中的每个文档,并找出最终的联接数据结构应该如何工作。正如Doug Stevenson指出的那样,Firestore没有内部联接查询,因此为了遵循这一思路,您必须编写自己的合并代码
但是,您最初的尝试是对Opportunity集合应用几个筛选子句,您可以立即实现。一种方法可能是:从三个查询中选择一个(为了提高效率,请选择返回最少文档的查询),然后过滤以删除与其他字段的所需值不匹配的文档。由于您是从同一个集合中检索文档,所以在SQL设置中通常理解的内部联接的思想对于这个用例来说是一种过度的工作
您还可以查看哪些字段直接允许基于多个字段进行查询