0

I am bit familiar with NoSQL and Firebase Realtime Database and also I know that it is not best solution to solve tasks where relational database should be more appropriate. I want to verify about structure of simple many to many relationship that I have.

I have events and users. I want to use Firebase for storing information about users participating in events, later I will need to

  • Get list of users for event knowing it's id and city
  • Get list of events for users knowing it's id and city
  • add or delete information about user attending to event

I would like to have first tree of events ids divided by cities.

events {
'city1' : {
           event_id_1 : {'user_1', 'user_2'},
           event_id_2 : {'user_3', 'user_4'}. 
           }
'city2' : {
           event_id_3 : {'user_5', 'user_6'},
           event_id_4 : {'user_7', 'user_7'}. 
           }
}

And second tree for users

users {
'user1' : {
           'city1' : {event_id_1, event_id_2}, 
           'city2' : {event_id_3, event_id_4},
           'city3' : {event_id_3, event_id_4}
           }, 
'user2' : {
           'city1' : {event_id_1, event_id_2}, 
           'city2' : {event_id_3, event_id_4},
           'city3' : {event_id_3, event_id_4}
           },
'user3' : {
           'city1' : {event_id_1, event_id_2}, 
           'city2' : {event_id_3, event_id_4},
           'city3' : {event_id_3, event_id_4}
           },
}

Would it be easy and fast to use and maintain?

moonvader
  • 19,761
  • 18
  • 67
  • 116

2 Answers2

1

You can do this:

List of user

 Users
  useruid 
    name:userx
    email:userx@gmail.com
  useruid
     name:usery
     email:usery@gmail.com

  Events
    eventid
       useruid
         name:userx
         location: city1
         eventname: party
    eventid2
       useruid1
          name:usery
          location: city2
          eventname: Boring Party

Get list of users for event knowing it's id and city:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Events").child(eventid);
ref.orderByChild("location").equalTo(city1);

//retrieve users using a listener

Get list of events for users knowing it's id and city:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Events");
Query q=ref.orderByChild("location").equalTo(city1);

using a listener this can give you the events that are in location:city1

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
1

Your structure looks pretty OK to me given the requirements listed. Most importantly: you store the data in both directions already, which is the biggest hurdle for many developers new to NoSQL data modeling.

A few notes about your data model, though most are on the level of typos:

  1. Be sure to store the data as maps, not arrays. So event_id_1 : {'user_1': true, 'user_2': true }
  2. If there is a many-to-many relationship between users and events, I'd usually have four top-level lists: users and events (for the primary information about each), and then userEvents and eventUsers (for connections between the two).

Adding a user to an event can be done with a single multi-location update, e.g.:

ref.update({
  '/userEvents/userId1/eventId1': true,
  '/eventUsers/eventId1/userId1': true
});

Unregistering them is a matter of doing the same with null as the value (which deletes the existing key):

ref.update({
  '/userEvents/userId1/eventId1': null,
  '/eventUsers/eventId1/userId1': null
});

Also see my answer here: Many to Many relationship in Firebase

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thank you, what is the point in storing true or null instead of just storing ids of entities? – moonvader Mar 06 '18 at 14:59
  • The second example *deletes* the keys. The first example stores `true`, since you can't store `null` and [Firebase recommends against using arrays for set-like structures](https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html) (also see [my answer here](https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value)). – Frank van Puffelen Mar 06 '18 at 17:07