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 stores product information, with three main properties:
$productId
/date
/category
/subcategory
On my website, I have three views:
- retrieve the last 4 products (orderBy date)
- retrieve last 4 products (by date) of category X
- retrieve last 4 products (by date) of category X and subcategory Y.
Note that I also have a node product_images, which have subnodes matching the productIDs. So constructing the databas as follows:
$categoryId
$subCategoryId
$productId
will not work as I need to know beforehand the $categoryId and $subCatrgoryId before I can match it with $productId. It would also make it difficult to retrieve the last 4 products.
How would I construct my noSQL database in an efficient way, and how would I retrieve the products with Firebase filtering it out with multiple restrictions?
I know that in Firebase you can use orderByChild and equalTo, but this only works on one restriction, whereas I have to deal with one to three.
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 callmulti-prop
. We'll also combine<category>_<subcategory>_<date>
into a property calledmegaprop
:Some sample data:
Now your queries turn into:
retrieve the last 4 products
retrieve last 4 products of category X
retrieve last 4 products of category X and subcategory Y.
Also see: http://jsbin.com/piluzo/edit?js,console for a working version.