firebase - 使用 arrayContainsAny 进行 Flutter Firestore 查询:列表不起作用
问题描述
最新版本:Flutter + cloud_firestore:^0.14.0+2
代码:
FutureBuilder(
future: _sessionsLogCollection
.where('companyId', isEqualTo: sPData.companyId)
.where('locationId', arrayContainsAny: [
'29L73oSzdQrzLUow3Mg9',
'bugdWVC6RRtHoemuxNWE',
])
// .where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE')
.orderBy('openDateTime', descending: true)
.get(),
我已经创建了索引,所以这不是问题。
单独使用.where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE')时,查询返回正确的数据。
但是单独使用.where('locationId', arrayContainsAny: ['29L73oSzdQrzLUow3Mg9','bugdWVC6RRtHoemuxNWE'])不会给出任何错误,而只是返回空数据集:sessionsSnapShot.data.documents.length: 0。
** 使用 .whereIn 解决(可能 .whereIn 应该包含在 firebase.flutter.dev/docs/firestore/usage 中,因为它目前不存在)
解决方案
根据您的代码.where('locationId', isEqualTo: 'bugdWVC6RRtHoemuxNWE')
,这似乎locationId
不是一个数组,而是一个字段。如果要查询数组,只能使用arrayContains
and arrayContainsAny
。但由于这是一个字段,因此您必须使用whereIn
:
.where('locationId', whereIn: [
'29L73oSzdQrzLUow3Mg9',
'bugdWVC6RRtHoemuxNWE',
])
使用 in 运算符可将同一字段上的最多 10 个相等 (==) 子句与逻辑 OR 组合在一起。in 查询返回给定字段与任何比较值匹配的文档
https://firebase.google.com/docs/firestore/query-data/queries#array_membership
推荐阅读
- java - 数组“ggg”是如何初始化的,什么是 .split()?
- javascript - 基于我是否关注用户的 MongoDB 搜索提要
- firebase - Cloud Firestore Firebase Collection ref 上的 IN 查询是否有 Flutter/Dart 等效项?
- excel - 循环以跨多个工作表向表中插入行
- java - 休眠错误访问字段,映射问题
- python - 如何通过 ATOMIC_REQUESTS 在多个数据库上使用 Django 的测试客户端?
- angular - 在侧边栏的路由角度中使用 URL 参数
- paypal - 在 PayPal 捐赠按钮上添加地址要求
- php - 如何从该类中重新实例化该类对每个函数调用的依赖关系?
- javascript - 检查域上是否存在网页,否则加载另一个页面