0

I am making an app for dog owners. I would like to make friendships among dogs. User can have n dogs and dog can have n friends. I am not sure how I should implement Firebase database in the most efficient way. I was thinking I would have a node friendships where I would store information friendship like this:

friendships:
     id_dog
        id_friend_dog1: true
        id_friend_dog2: true

I need to get friends of the current dog. Also I need to be able to get other dogs that arent friend yet. How would you implement this use case?

Thanks

user3637775
  • 499
  • 4
  • 20

1 Answers1

0

How you model your data in Firebase usually depends on how you want to use that data in your app.

In your scenario there's an extra interesting question: is friendship bi-directional by nature? In other words: if dog1 is a friend of dog 2, does that automatically mean that dog2 must be a friend of dog1? If so, you'll want to model it the way I described in Best way to manage Chat channels in Firebase

friendships:
    id_dog1-id_dog2: true
    id_dog1-id_dog3: true

In addition you'll want to do as you already said: keep a list of friends for each dog.

You'll see that this means you're keeping duplicate data: both a list of friends for each dog and a "master list" of friendships between dogs.

This sort of data duplication is common in NoSQL databases and Firebase is no exception.

Update (for comment)

Data modeling in a NoSQL solution always depends on how you want to use your data. If you want to get a list of friends for a specific dog, you should store a list of friends for each dog.

dogFriends
  dog1
    dog2: true
    dog3: true
  dog1
    dog1: true
  dog3
    dog1: true

Now you can easily find out the friends of a specific dog by loading /dogFriends/{dogId}. Then loop over those results and load the details of each friend.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Friendship should be bi-directional. If I use my structure how would I be able get all information of dog friends? I can their ids. Do I have to iterate through it? Or how would you do it? I need to get photo, name and breed of dog friend. – user3637775 Nov 26 '16 at 07:53
  • Thanks. Now I am getting friends of the current dog even in the fast time relative to my another post. Im just curious how I can get the dogs that arent friends with the current dog. I looked at your answer in this post http://stackoverflow.com/questions/39195191/firebase-query-to-exclude-data-based-on-a-condition/39195551#39195551 I am not sure if it is possible because I need to ask if the id is not equal to the current dog. I would need to filter by orderByKey. Do you know about some solution? I was thinking about creating a subset but its not really cool solution. It may take time.Thanks – user3637775 Nov 27 '16 at 18:21
  • As the answer you linked to says, you cannot test for inequality. So you'll have to find another way, such as client-side filtering. – Frank van Puffelen Nov 27 '16 at 18:36
  • Ok. I will get of all dogs then remove the friends. – user3637775 Nov 27 '16 at 18:39