【发布时间】:2020-04-23 09:00:05
【问题描述】:
我的应用提供了基于州/位置查询高尔夫球场的能力——由于数据模型的原因,我需要从 Firestore 中的两个不同集合中提取数据。目前收藏(“golfclub”包含位置信息和“课程”,其中包含我需要检索的课程名称)。
这两个集合共享一个公共字段(“设施 ID”)并具有多对一(课程 -> 高尔夫俱乐部)关系。因为在每个 Golfclub 中可能有 1 个或多个与之相关的球场。
我将如何创建一个子集合(clubcourses),根据匹配的设施 ID,将现有课程集合中的每个文档附加到 golfclub 集合中的相应文档?
我的最终结果是启用类似于集合组查询文档中列出的每个城市的地标查询的查询。 https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query
Dart 中没有集合组查询的现有查询;
void getFSCourseList() {
List<String> facility = [];
var clubQuery = golfClubDatabase
.collection("golfclub")
.where("State", isEqualTo: "$_selectedState");
clubQuery.getDocuments().then((clubSnap) {
if (clubSnap.documents.length > 0) {
print('club docs length = ${clubSnap.documents.length}'); // sanity check
clubSnap.documents.forEach((document) {
facility.add(document.data[
"Facility ID"]); //add the facilities to a temp list for use in next query
});
var courseQuery = golfCourseDatabase
.collection("course")
.where("Facility ID", arrayContainsAny: facility); // doesn't return a value for some reason...
courseQuery.getDocuments().then((courseSnap) {
print('length of course snap = ${courseSnap.documents.length}'); // sanity check should be longer than clubSnap.length
if (courseSnap.documents.length > 0) {
print('length of course snap = ${courseSnap.documents.length}');
}
});
facility.forEach((fac) => print(fac)); // sanity check should equal club-snap.length
//print(facility.length);
} else {
print("Not Found");
}
});
}
【问题讨论】:
标签: dart google-cloud-firestore