I need best practice tips to enhance the performance of my noSQL database (hosted on Firebase). Moreover, I need tips on how to structure the nodes.
The database sto
Since Firebase only can filter on one property, you'll have to combine the values you want to filter on into a single property. Since you have multiple filtering needs, you might need such a property for each filtering use-case.
It seems that this will be possible for you if you combine <category>_<date>
into a single property, which we'll call multi-prop
. We'll also combine <category>_<subcategory>_<date>
into a property called megaprop
:
$productId
/date
/category
/subcategory
/multiprop
/megaprop
Some sample data:
id_cc_1234
date: 20151031
category: candy
subcategory: halloween
multiprop: candy_20151031
megaprop: candy_halloween_20151031
id_tg_2345
date: 20151125
category: candy
subcategory: thanksgiving
multiprop: candy_20151125
megaprop: candy_thanksgiving_20151125
id_tg_3456
date: 20151125
category: food
subcategory: thanksgiving
multiprop: food_20151125
megaprop: food_thanksgiving_20151125
id_sk_4567
date: 20151205
category: candy
subcategory: sinterklaas
multiprop: candy_20151205
megaprop: candy_sinterklaas_20151205
id_sc_5678
date: 20151225
category: candy
subcategory: christmas
multiprop: candy_20151225
megaprop: candy_christmas_20151225
Now your queries turn into:
retrieve the last 4 products
ref.orderByChild('date').limitToLast(4);
retrieve last 4 products of category X
ref.orderByChild('multiprop').startAt('candy').endAt('candy_~').limitToLast(4);
retrieve last 4 products of category X and subcategory Y.
ref.orderByChild('megaprop').startAt('candy_christmas').endAt('candy_christmas_~').limitToLast(4);
Also see: http://jsbin.com/piluzo/edit?js,console for a working version.