1

I have a big database in Firebase. In my opinion it's wrong structured. Trying to get a snapshot of reference, It takes too much time processing the results. However, I don't need to retrieve all data. I don't need the deepers items but I don't know how can I get the superficials.

So the structure is something like this:

clients {
    1234 {
      name: 'Paul',
      last_name: 'Mcartney',
      city: 'Liverpool',
      account_movements: {
           item1: {
              ...
           }
      }
    }
    1234 { ... }
    1236 { ... }
}

Let's supose that what I need is all clientes data except his account_movements.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Marcelo J Forclaz
  • 718
  • 1
  • 8
  • 27
  • 1
    When you read data from Firebase, you always get full nodes. There no way to get a subset of the properties of each node, nor is there a way to only get the keys. Also see https://stackoverflow.com/questions/37365866/firebase-2016-shallow-query – Frank van Puffelen Sep 14 '17 at 00:13
  • 1
    This requirement typically means that you should denormalize your data further. For example: you should probably remove the `account_movements` out from under each client into their own top-level nodes. – Frank van Puffelen Sep 14 '17 at 00:15

2 Answers2

1

Because you are talking about a big database i suggest you restructure your database a little bit. As Frank says, when you are trying to read a node, you get the entire node. This means that every time you add a listener to specific reference, you are downloading the entire information of that object. Note, that there is no way in which you can download only a subset of the properties of each node and that's why you need to denormalize your database. Because an important rule in Firebase is to have the database as flatten as possible, i suggest you get the account_movements out from each user and add it separately as a top-level node. To understand better, i suggest you read this post, Structuring your Firebase Data correctly for a Complex App and see this tutorial, Denormalization is normal with the Firebase Database.

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks @Alex_Mamo for your information. I suspected that denormalization would be the only solution. Your answer finished to confirm that. – Marcelo J Forclaz Sep 14 '17 at 15:57
0

You can either create new nodes "account_movements" with their own ids and add them to user or you can create another node for users that doesn't have details about users. Second solution requires more space, but it is easier to make read operations as you only need to create 1 call (user with details or without) instead of reading id of account_movements to create second call. In this example you can name id of account_movements with user's id. But that's not always possible

Janusz Hain
  • 597
  • 5
  • 18