1

I am new in noSQL and Firebase. But I want to build the structure of my database via Firebase.

I have users and a list of users lots. Structure with relationship. So I did what's in the example:

 String key = mDatabase.child("lots").push().getKey();
    //create new lot
    Lot lot = new Lot(fbUser.getUid(), fbUser.getEmail(),  mMessage.getText().toString());
    //turn to map
    Map<String, Object> lotValues = lot.toMap();
    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("/lots/" + key, lotValues);
    childUpdates.put("/user-lots/" + fbUser.getUid() + "/" + key, lotValues);
    mDatabase.updateChildren(childUpdates);

But in the result I had this data to duplicate:

this

May be it's better to get this structure. I tried to find an example how to build one, because I do not want to to reinvent a wheel, but my efforts are futile so far.

Any suggestions? Thanks a lot.

Community
  • 1
  • 1
Serg Burlaka
  • 2,351
  • 24
  • 35
  • 1
    If I understood correctly, you want to have a pointer to another node in the database? If so that's not possible, don't be surprised, Firebase has TONS of missing important features such as collection filtering, one-to-one upstream messages, and this. – Ali Bdeir Dec 09 '16 at 11:11
  • @AbAppletic so, as example, if i want to get data such as "best-user-lot" in one request, i must to create a separete branch for data filling, like this: /best_user_lots/user_id/lots_id ? – Serg Burlaka Dec 09 '16 at 11:26
  • Yes. You do. It's very unfortunate that Firebase doesn't provide a way to do it without excess data, but it has to be done the way you're currently doing it. – Ali Bdeir Dec 09 '16 at 11:28
  • @AbAppletic Oh thanks a lot! I am trying second day to to understand structure in firebas, and now I have finished, may be)) – Serg Burlaka Dec 09 '16 at 11:34
  • Good luck then :) – Ali Bdeir Dec 09 '16 at 11:38

1 Answers1

1

What you're looking for is Pointers, which Firebase, believe it or not, DOES NOT have.

For example, if you want to have 3 lists of data:

  1. My Posts
  2. Recent Posts

Then you'll have to do it like this:

databaseRoot:{
  Posts:{
     {UNQ_KEY}:
      {
        title: "x",
        description: "y",
        authorUserID: {USERID1}
      }
   }

  Users:{
    {USERID1}
      Posts:{
        {UNQ_KEY_2}: {
         title: "x",
         description: "y"
        }
      }
   }
}

When UNQ_KEY is created, you also create UNQ_KEY_2 under the user's userID.

To display "My Posts", you get the list under {USERID1}. To get "Recent Posts", you have to go to the Posts node.

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117