1

I have the following structure in my Firebase database:

- Items (L1)
  -- key (L2)
    --- itemId (L3)
    --- itemName (L3)
    --- ....
- Orders (L1)
  -- key (L2)
    --- key (L3)
       ---- itemId (L4)
       ---- itemName (L4)
       ---- itemQty (L4)

I have the following index setup for orders.

    "orders": {
      "$custId": {
         ".indexOn" : "itemId",
        "$orderId" : {
          ".indexOn" : "itemId"
        }
      }
    },

I would like to pull all orders for a specific itemId (e.g. 1011). I have tried to leverage the Google dinosaur example. But, I couldn't get it to work perhaps because I have two key levels in my order structure.

I would like to write the request in as a RestAPI such as: var url = 'https://xxxx.firebaseio.com/orders.json?orderBy="itemId"&equalTo=1011';

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • You won't be able to get them all in one query. You'll need two, one for Items and another for Orders. – Doug Stevenson Aug 31 '20 at 03:37
  • In your current structure you can find all orders for a single customer with a specific item in them. You can't search all orders for all customers though. 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 Aug 31 '20 at 03:56
  • Thanks Doug - yes, I would know the itemId when I submit the query for orders with the itemId. – user13831626 Aug 31 '20 at 21:47
  • Thanks Frank - based on your links and prior readings, it sounds like I am not able to get all the orders containing a specific item based on my data structure. This said, I am still not sure why I can't achieve my objective. In the following - https://stackoverflow.com/questions/51382047/firebase-rest-orderby-nested-child-of-autogenerate-id - the requester received an answer to search through a 'db generated key' level of the hierarchy. I want to accomplish almost the same - just through two 'db generated key' levels. If my comments don't make sense, I am happy to try to explain differently. – user13831626 Aug 31 '20 at 22:00
  • Based on reading other examples and these comments, it appears Firebase can only read one level down from a known value. Since I have two levels of dynamic data, I am not able to accomplish my goal. I will need to restructure my data. If a solution exists without rearranging my data model, please respond. Otherwise, I deem this closed. Thanks BC – user13831626 Sep 01 '20 at 15:06

0 Answers0