0

This is my firebase database structure in the image :

enter image description here

I'll explain the structure aswell, I have a forum, which people can post trades in. Every post has some random user key as you can see in the picture. Also every post has a list of items which the user wants to sell or buy ('have' or 'want', in the image). the number of items in each trade can change from 1 to 10.

I want to fetch all of the forum posts of the users that are selling ( or buying ) with some item id.

For example : Get forum posts of users selling items with 'Id' of 'Some Item Name'

How do I do this? I can't seem to get reference to each item in the inventory since you can't do multiple orderByChild. If you think it's impossible with this DB structure, Please offer me an alternative. I want to know whether I'm wasting my time with firebase and this is impossible to do :(

Please note that I have a lot of data in my DB so I can't just filter the posts on the client side.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • It looks like the current database structure indeed doesn't allow the query you want. Have a look at https://stackoverflow.com/questions/27207059/firebase-query-double-nested and http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value for related problems. – Frank van Puffelen Sep 02 '17 at 13:09
  • Thank you frank, Will it be easier using SQL tables and drop firebase? or is it complex to do there aswell.. It seems like ill need to a post for each of the items the person have in the inventory to solve it.. – Hen Eliezer Sep 02 '17 at 17:19

1 Answers1

1

Either you can change your database structure OR You can create a different node which can contain the metadata of all the "have" OR "want" items with the itemID, userID and "have" or "want" node child number(in this case it should be 0-9, as there are 10 items in each type). So whenever you are saving/deleting you data from "have" or "want" section you have to do the same operation in the other new metadata table also.

You can run your query on this node to get the desired item and then with the help of result data you get those particular items directly by creating a reference at runtime as you are having userId, have or want type, itemId.

your metadata should be something like.

metadata
  |
  |
  +{randonId1}
       |
       |-type : "have" or "want"
       |-userId : "randonId of user".(Kt0QclmY3.as shown in picture)
       |-Id: "Breakout Type-S"
       |-childOnNode: 0, (0-9)
 +{randonId2}
       |
       |-type : "have" or "want"
       |-userId : "randonId of user".(Kt0R48Cp..as shown in picture)
       |-Id: "Breakout"
       |-childOnNode: 0, (0-9)
Morya
  • 346
  • 1
  • 11
  • I Appreciate your comment, But let's say I searched for an id 'Breakout Type-S' and I got like 20-30 results of user ids that is selling it, won't i need to create a new query for every id that I get? that means 20-30 querys.. or is there a way to do it with one query or two? – Hen Eliezer Sep 02 '17 at 08:13
  • @HenEliezer, Yes that will be 20-30 queries but since you have the exact end point of your data which make your query faster and you are not doing any write operation here but just a read operation which is really, moreover if you want to optimise it you can always do the paging and show the users limited data and not overloading the firebase database. – Morya Sep 04 '17 at 14:27