0

In my app I want to give the users the possibility to make their own lists. For the sake of a easy example, imagine your standard list app. The users can create their own custom lists, and put what ever they want in it. I want the users to be able to search all lists to see if an item is in it. For example milk could both be in a grocery list, and a recipe for pancakes. So in this example I would like to search all the lists a user has made, and get back the name of the lists that contains milk

I currently have a Firebase structure like this:

   {
    "userLists" : {
        "vBbYFkxrj8UTmBiUxKsjNSw8YBs2" : {
          "groceryList" : {
            "82654829" : {
              "amount" : "1",
              "objectName" : "Milk"
            },    
             "72819278" : {
              "amount" : "1",
              "objectName" : "Banana"
            },
             "83917362" : {
              "amount" : "2",
              "objectName" : "Apple"
              }    
            },
              "pancakeRecipe" : {
            "12986530" : {
              "amount" : "1",
              "objectName" : "Milk"
            },
               "1087527" : {
              "amount" : "1",
              "objectName" : "Flour"
            },
             "10985255" : {
              "amount" : "1",
              "objectName" : "Egg"
            }
           },
           "To do" : {
            "12984762" : {
              "objectName" : "Mow lawn"
            },
            "12984762" : {
              "objectName" : "Wash car"
            },    
             "98129854" : {
              "objectName" : "Pay bills"
            },
          }
         },
      "users" : {
        "vBbYFkxrj8UTmBiUxKsjNSw8YBs2" : {
          "Address" : "Kings Road 123",
          "Email" : "a@a.no",
          "Name" : "John Doe",
          "Phone" : "11223344"
        }
      }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Anvil
  • 1,745
  • 1
  • 11
  • 16
  • Firebase Realtime Database queries work on a flat list, not on a hierarchy of lists. So you'll either have to flatten the data structure (as Jay answered) or do a separate search for each list (as Yip answered). Also see https://stackoverflow.com/questions/27207059/firebase-query-double-nested and https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen May 19 '19 at 20:33

2 Answers2

3

The simplist solution is to change the structure to match the type of query you want, and/or add an additional structure(s). This is a very common technique in NoSQL databases.

lists
   list_id_0
      name: "Grocery List"
   list_id_1
      name: "Pancake Recipe"
   list_id_2
      name: "To Do"

object_lists
  autoId_0
     amount: 1
     objectName: "Milk"
     list_id: "list_id_0"
  autoId_1
     amount: 1
     objectName: "Banana"
     list_id: "list_id_0"
  autoId_2
     amount: 1
     objectName: "Apple"
     list_id: "list_id_0"
  autoId_3
     amount: 1
     objectName: "Milk"
     list_id: "list_id_1"
  autoId_4
     amount: 1
     objectName: "Flour"
     list_id: "list_id_1"

and with this structure, querying for all objects for objectName of Milk is easy and that will also tell you which list_id it belongs to.

You could even go so far as to give your objects an id, and tie them to lists that way

objects
  object_id_0
     name: "Milk"
  object_id_1
     name: "Flour"
  object_id_2
     name: "Banana"

lists_of_objects
   list_id_0
      object_id_0: true
      object_id_1: true

then a query for every list where object_id_0 has a true value

Oh but wait.. there's more! Check this out

objects_and_their_lists
   object_id_0  
      list_id_0: true
      list_id_1: true

and with that structure, if you know the object_id (milk in this case) you can just grab object_id_0 from this structure and you know exactly which list it appears in. That would avoid a query completely and is very lightweight.

Jay
  • 34,438
  • 18
  • 52
  • 81
2

You will have to do linear search on all lists

Search groceryList for each item to see if any object is named "Milk": 82654829 Yes, 72819278 No, ....

Search pancakeRecipe for each item to see if any object is named "Milk":

Search "To do" for each item to see if any object is named "Milk"

Yip Cubed
  • 56
  • 3