Database modeling Firebase: Relations n: n

3

I'm studying firebase using NoSQL and I had a question while watching this video: Converting SQL Structures to Firebase Structures - Firebase Database for SQL Developers # 2

In it the author creates a structure like this:

Blz...withthisstructureitwouldbeveryeasytoreturnallparticipantsofagiveneventjustbytraversingtheeventAttendeesnode.

ButwhatifIwanttoknowalltheeventsthataparticularuserparticipatesin.HowwouldIdoit?IfIcreateanothernodeusersAttendeesIcoulddothisbysavingtheusersandwithineachusertheeventsthatitparticipates.

Buttherewehaveaproblem:duplicationofdata.Ifthereisamodification,say,ifIremoveaneventfromuserDavid,I'llhavetoremovethetwonodes:eventAttendeesandusersAttendees.

ThiswouldbeeasilydoneusingSQLmodelingasinthephotobutthegoalhereistodousingNoSQL:

So my question is, what is the best way to model this in NoSQL to avoid this kind of problem?

EDIT : I realized that this problem persists for any relation n to n. So by specifying more, my question would be how to do n to n relationships with NoSQL in the best possible way?

    
asked by anonymous 03.11.2017 / 15:13

1 answer

0
  

But what if I want to know all events that a particular user   participates. How would I do it?

You can make an appointment without having to change your structure. For example, to see all the events that David participates in:

var eventAttendeesRef = firebase.database() .ref().child("eventAttendees");
eventAttendeesRef.orderByChild('1').equalTo('David').once('value', function(dataSnapshot){
    dataSnapshot.forEach(function(snapshot){
        var eventoID = snapshot.getKey();
        console.log('O David participa do evento com ID: '+eventoID);
    });
});
  

How to make n to n relationships with NoSQL in the best possible way?

NoSQL does not have a standard way of doing n to n relations. As you may have seen in this series of Youtube videos, you have to structure your data according to your View. Always try to find a structure that reduces the number of queries needed to get a die. Nor do you need to duplicate some data in some situations.

    
24.02.2018 / 17:53