0

I need to make a query for my database in Realtime Firebase. It is my database structure: View

json:

{
  "Recepts" : [ {
    "Ingridients" : [ "Carrot", "Sugar" ],
    "Name" : "Carrot Salad"
  }, {
    "Ingridients" : [ "Orange", "Milk" ],
    "Name" : "Orange Milk"
  } ]
}

How can I do a query, I make code like this:

rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference resultRef = rootRef.child("Recepts");
Query resultQuery = resultRef.orderByChild("Ingridients"); 

But I can not add ValueEventListener for Query object. What do I need to do?

upd How can I structure my database in Firebase for search for recipes? Each recipe have some ingridients. I need to search for recipes. If the query contains several matching ingredients, Ineed to output a recipe containing a matching value

  • Firebase can query the children of a specific location if the property is at a known path in the JSON. As you can see in your screenshot the ingredients are not at a known path, because they may be at any index in the array. That's why arrays are not a good structure to store such a collection of ingredients. Instead consider storing them as a map, which prevents having duplicate ingredients in a recipe. Then also store the inverse data structure, from an ingredient to the recipes that use that ingredient. See my answer here: https://stackoverflow.com/a/40657455 – Frank van Puffelen Apr 06 '18 at 13:39
  • Don't update a question closed as a duplicate to ask another question; you won't get any answers for it. Ask a new question instead. – yole Apr 07 '18 at 06:15

0 Answers0