替代 select distinct in firestore flutter
Alternative to select distinct in firestore flutter
是否有任何方法可以使用 collectionGroup
查询(如 SELECT DISTINCT
)获取 firestore 中的所有唯一字段值?
fetchAvailableZipCode() {
try {
_availableZipCodeStream = FirebaseFirestore.instance
.collectionGroup(PLACES_COLLECTION)
.orderBy('cityZipCode', descending: true)
.snapshots()
.listen((event) {
List<String> currentAvailableZipCode = [];
event.docs.map((e) {
if (!currentAvailableZipCode
.contains(e.get('cityZipCode').toString()))
currentAvailableZipCode.add(e.get('cityZipCode').toString());
}).toList();
_availableZipCode.value = currentAvailableZipCode;
print(_availableZipCode.toString());
print(_availableZipCode.length.toString() + ' available');
});
} catch (e) {
Get.snackbar('app_error'.tr, 'app_error_description'.tr);
}
}
Firestore 没有像 select distinct 这样的运算符,也没有任何其他数据聚合操作。它 returns 与存储的数据完全相同。
也就是说,如果你想获取某个字段的不同值,你有两种选择:
- 从数据库中检索所有文档并确定应用程序代码中的不同值。
- 使用附加集合中的不同值作为文档键,这样您就可以在需要不同值时读取该集合中的所有文档。
后者在 Firestore 和许多其他 NoSQL 数据库中是惯用的:您最终会更改数据模型并复制数据以适应应用程序的特定用例。
是否有任何方法可以使用 collectionGroup
查询(如 SELECT DISTINCT
)获取 firestore 中的所有唯一字段值?
fetchAvailableZipCode() {
try {
_availableZipCodeStream = FirebaseFirestore.instance
.collectionGroup(PLACES_COLLECTION)
.orderBy('cityZipCode', descending: true)
.snapshots()
.listen((event) {
List<String> currentAvailableZipCode = [];
event.docs.map((e) {
if (!currentAvailableZipCode
.contains(e.get('cityZipCode').toString()))
currentAvailableZipCode.add(e.get('cityZipCode').toString());
}).toList();
_availableZipCode.value = currentAvailableZipCode;
print(_availableZipCode.toString());
print(_availableZipCode.length.toString() + ' available');
});
} catch (e) {
Get.snackbar('app_error'.tr, 'app_error_description'.tr);
}
}
Firestore 没有像 select distinct 这样的运算符,也没有任何其他数据聚合操作。它 returns 与存储的数据完全相同。
也就是说,如果你想获取某个字段的不同值,你有两种选择:
- 从数据库中检索所有文档并确定应用程序代码中的不同值。
- 使用附加集合中的不同值作为文档键,这样您就可以在需要不同值时读取该集合中的所有文档。
后者在 Firestore 和许多其他 NoSQL 数据库中是惯用的:您最终会更改数据模型并复制数据以适应应用程序的特定用例。