javascript - firebase 的多个单一结果
问题描述
我有一个包含一些 id 的元素数组。我需要连接到 firestore 并仅获取具有每个指定 id 的记录。
我的数组:
var ids = { 101, 201, 303}
和我的firestore文件:
{
"users": {
{
"id": 1,
"name": "name1",
"otherData:" "otherData"
},
{
"id": 2,
"name": "name1",
"otherData:" "otherData"
},
{
"id": 3,
"name": "name1",
"otherData:" "otherData"
},
...
{
"id": 1000,
"name": "name1",
"otherData:" "otherData"
}
}
}
如何使用 db.collection('coll1').where() 语句有效地做到这一点?我尝试使用 forEach 来获取数据,如下所示:
ids.forEach(id => {
let result = db.collection('coll1').where('id', '==', id).get();
...
});
但是每次我尝试这样做时,它都不起作用。
我是 firestore 环境的新手,不知道如何进行这样的操作。请帮忙。
解决方案
您可以为此使用 Firstore 复合查询。此处链接到官方文档。
使用 in 运算符可将同一字段上的最多 10 个相等 (==) 子句与逻辑 OR 组合在一起。in 查询返回给定字段与任何比较值匹配的文档。
同样,使用 array-contains-any 运算符将同一字段上的最多 10 个 array-contains 子句与逻辑 OR 组合在一起。数组包含任何查询返回文档,其中给定字段是包含一个或多个比较值的
数组您需要将 id 更改为数组类型
var ids = [ 101, 201, 303 ]
询问
db.collection("coll1").where("id","in", ids).get();
推荐阅读
- javascript - Javascript 无法导入现有常量
- python - 具有共享 x 轴的 Seaborn 图
- javascript - 在 Twilio 函数中获取入站短信号码
- javascript - 在许多不同的表(谷歌表)中使用来自一个谷歌脚本的函数
- android - 如何更改文本字段的颤动默认复制/暂停/选择菜单?
- java - Eclipse IDE 版本 2019-09 R (4.13.0) 无法识别 Maven 依赖项
- python - 视频输入上的 TFLite 推理
- python-3.x - 熊猫:与其他两列的唯一值对相对应的列的总和值
- generics - 如何在 Rust 中传递具体(非类型)通用参数?
- c++ - 修复我自己的双向链表中的内存泄漏