Wednesday, 7 June 2017

MongoDB Many to Many Two ways of Implementation

I am using MongoDB (and mongoose) for storing my data about: Clubs & Tracks.

As you might guess:
Club has many Track-s.
Track might be in many Club-s.

I know two ways to save them into DB. First being:
Club { name: String, tracks: [Track] // Track Schema be defined similarly above it. But! it won't have a separate table, correct? }
Whereas, second would be:
Club { name: String, tracks: { type: Schema.Types.ObjectId, ref: 'Track' } // This means Track will have a separate table, correct? }

Now, the following requests(queries) to database will have, more or less, equal frequency. And will look something like this:
1. get tracks by club_id
2. search tracks by name
3. search clubs by track, etc

I believe it won't be a healthy approach to have same tracks in different places over and over again (which is likely to happen in case of the first approach).
I think, first approach would be right way if I had blog and comments.

What way do you suggest and why?



via levi

No comments:

Post a Comment