I am trying to use Firebase to set up a new project, and I am struggling with a non-relational database and query system. I read recommendations, especially regarding data structure. Here is my use case: I have to process some products, and each of them has a name and a family, and this is also an entity. I created the structure below, trying to follow the recommendations and examples.
{
"families" : [ {
"name" : "easy",
"products" : [ 10, 11 ]
}, {
"name" : "ir33",
"products" : [ 12, 13 ]
} ],
"products" : {
"10" : {
"family" : 0,
"name" : "Matita"
},
"11" : {
"family" : 0,
"name" : "Penna"
},
"12" : {
"family" : 1,
"name" : "Gomma"
}
}
}
Given the family, I would like to select all the products of this. My first solution is to access the family, retrieve an array of identifiers, and then execute queries (one for each identifier). It works, but this approach potentially leads to a lot of queries. Is there a better way to get this? For example, I try with this, but it does not work:
ref.child("products").queryOrderedByChild("family").queryEqualToValue("0", childKey: "family").observeSingleEventOfType(.Value, withBlock: { snap in
print("snap \(snap)")
})
Snap (products) <null>.
UPDATE
:
ref.child("products").queryOrderedByChild("family").queryEqualToValue(0).observeSingleEventOfType
Btw, :
[FirebaseDatabase] . ".indexOn": "family" at/products
!