You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Firestore查询:如何获取大于5的整数值norequest文档?

实现方案

Firestore本身没有直接支持「判断字段是否为10的倍数」的查询操作符,你可以根据实际场景选择以下方案:

方案一:已知目标值范围(比如仅需10、20、30等有限个值)

如果需要匹配的10的倍数固定且数量不多(最多10个),直接用whereIn列出所有符合条件的值即可:

QuerySnapshot querySnapshot = await FirebaseFirestore.instance
    .collection('stations')
    .where('norequest', whereIn: [10, 20, 30, 40]) // 按需添加更多10的倍数
    .get();

因为这些值本身都大于5,所以不需要额外加isGreaterThan:5的条件。

方案二:目标值范围无限(需匹配所有10的倍数且大于5)

这种情况没法用whereIn(最多支持10个值),最高效的方式是添加辅助字段:
在stations集合的每个文档中新增一个布尔字段(比如isTenMultiple),当norequest是10的倍数时设为true,否则设为false。之后就可以这样查询:

QuerySnapshot querySnapshot = await FirebaseFirestore.instance
    .collection('stations')
    .where('isTenMultiple', isEqualTo: true)
    .where('norequest', isGreaterThan: 5)
    .get();

方案三:客户端过滤(不推荐,仅临时应急)

如果不想修改文档结构,可以先查询所有norequest>5的文档,再在客户端过滤出是10的倍数的结果:

QuerySnapshot querySnapshot = await FirebaseFirestore.instance
    .collection('stations')
    .where('norequest', isGreaterThan: 5)
    .get();

// 客户端过滤
List<DocumentSnapshot> targetDocs = querySnapshot.docs.where((doc) {
  int? value = doc.data()?['norequest'] as int?;
  return value != null && value % 10 == 0;
}).toList();

注意:这种方法会下载所有norequest>5的文档,数据量大时会增加带宽成本和加载时间,仅适合数据量极小的场景。

内容的提问来源于stack exchange,提问作者Afthal Ad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:05:24