我想从FireBase数据库中获取特定项目,例如,我的FireBase中有带有汽车的数组,每辆汽车都有一个唯一的ID,我只想下载按ID搜索的特定汽车,并且想将其解析为Car java对象,而无需下载所有不必要的汽车清单。
An alternative to your self-answer is to store the cars under their id:
cars
car_1
make: ...
model: ...
car_2
make: ...
model: ...
Then you can access the cars without needing a query, which means things will scale a lot better:
var id = 2;
ref.child('cars').child('car_'+id)...
Note that it is considered a bad practice to store array indices as keys in Firebase, which is why I prefixed the ids with car_
.
First go to specific list of objects which you want to search using .child("cars")
, after that get specific element you want to campare in this example is carId, .orderByChild("carId")
and finaly check if its value equals to your .equalTo("2")
. The final result is fireBaseRef.child("cars").orderByChild("carId").equalTo("2");
this will return all cars with id of 2. If you know better way please write here :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.