0👍
It may help to keep firebase more shallow ie. allow plants to be the top-level document and give each plant sub-properties for collection and plant tags. This should allow orderbyChild to order all top-level plants by their child plantTags = water, for example.
- [Vuejs]-Using v-for range on attributes in vue.js
- [Vuejs]-Deploy Vue.js Okta Authentication app on Netlify
0👍
As Cuttsy27 proposed, you could denormalize your data. A good approach for implementing your requirements, would be to have, in addition to the collections
node, an extra main node plantTags
and have a child node for each plantTag with plants children nodes with the needed info:
- collections
- ....
- plantTags
- water
- 1082 <- plantID
- plantName: "........"
- plantPic: "xxxxxxxx.png"
- 362
- plantName: "........"
- plantPic: "xxxxxxxx.png"
- propagate
- 362
- plantName: "........"
- plantPic: "xxxxxxxx.png"
- 876
- plantName: "........"
- plantPic: "xxxxxxxx.png"
In such a way it would be very easy to query for all the plants which have a given tag.
For example in “pure” JavaScript:
var tagToQuery = 'water';
var ref = database.ref('/plantTags/' + tagToQuery);
ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
console.log(childData.name)
// ...
});
});
In order to maintain the two nodes in sync, use the update
method (documented here: https://firebase.google.com/docs/database/web/read-and-write#update_specific_fields) which allows to “simultaneously write to specific children of a node”
(Simple) example with your data structure:
var updates = {};
updates['/collections/0/plants/0'] = ... ;
updates['/plantTags/water/362] = ...;
updates['/plantTags/propagate/362] = ...;
return firebase.database().ref().update(updates);
0👍
Don’t use Array
-> Use Dictionary
instead. Search can not be perform in Array
. Instead of 0, 1, 2 use autogenerated keys everywhere.
Then you can query like
collections -> collectionId -> plants -> yourQuery
Or redefine your db structure something like you can fetch this content. With current db its not possible to fetch the data until you know the each index of collections
(0, 1 or else)
and plants
(0, 1 or else)
to reach at the node.