【问题标题】:How to download specific item from FireBase, but not all list with items?如何从 FireBase 下载特定项目,但不是所有项目列表?
【发布时间】:2015-12-28 14:11:02
【问题描述】:
我想从我的 FireBase 数据库中获取特定项目,例如,我的 FireBase 中有包含汽车的数组,每辆车都有一个唯一的 ID 我只想下载一辆按 ID 搜索的特定汽车,我想将其解析为 Car java 对象,无需下载所有不必要的汽车列表。
【问题讨论】:
-
equalTo() 呢?从firebase 文档中,您可以找到更多信息here
-
标签:
java
android
firebase
firebase-realtime-database
【解决方案1】:
您自己回答的另一种方法是将汽车存储在他们的 ID 下:
cars
car_1
make: ...
model: ...
car_2
make: ...
model: ...
然后你可以在不需要查询的情况下访问汽车,这意味着事情会更好地扩展:
var id = 2;
ref.child('cars').child('car_'+id)...
请注意,将数组索引存储为 Firebase 中的键被认为是一种不好的做法,这就是为什么我在 id 前面加上 car_。
【解决方案2】:
首先使用.child("cars") 转到您要搜索的特定对象列表,然后在此示例中获取您想要campare 的特定元素是carId、.orderByChild("carId"),最后检查它的值是否等于您的.equalTo("2") .最终结果是 fireBaseRef.child("cars").orderByChild("carId").equalTo("2"); 这将返回所有 id 为 2 的汽车。如果您知道更好的方法,请写在这里 :)