Denormalized Many to many relation between collection

Hello everyone.
I am creating an application for events.
I have several data models

  1. Event
  2. Guest
  3. Organizers

There is a many to many relation between an event and a guest.
Each guest can register to many events and each event can have many guests (I’m talking about the hundreds, tops a thousand)
The thing is that I want to be able to query what events a specific guest is going to and what guests are in a specific event, both requires the name ( a list of names of events a guest is going and vice versa).
I’ve chose to have 2 collections (guests and events) and have each document have the respective id’s but because a common query is to get the names I’ve decided to refactor the model to be normalized meaning each guest have a list of basic events (objects that have name and id).

{
  "_id": {
    "$oid": "66b865d7efca7ad579233e4d"
  },
  "name": "Christiansen - Kihn",
  "dressCode": "Only white",
  "location": "Genovevachester",
  "guests": [
    {
      "_id": {
        "$oid": "66b9f775186812e09f2b3412"
      },
      "name": "Liel Shalom"
    },
    {
      "_id": {
        "$oid": "66b9f775186812e09f2b3411"
      },
      "name": "Liel Almo"
    }
  ],
  "date": {
    "$date": "2024-08-11T07:18:47.801Z"
  },
  "isVipEvent": true
}

And vice versa to the guests collection

{
  "_id": {
    "$oid": "66b9f775186812e09f2b3412"
  },
  "name": "Skiles, Stark and Frami",
  "events": [
    {
      "_id": {
        "$oid": "66b865d7efca7ad579233e4d"
      },
      "name": "Christiansen - Kihn"
    }
  ],
  "isVip": true,
  "age": 300
}

Now, I’ve search for several solution in the wild and seen the `subset’ pattern but I do not think this is the right pattern for me.
I’d love any help

Hi guys, I’d love if someone could help me

Applying subset pattern on guest collection isn’t a good idea . Events are unbounded array , that can grow indefinetly. But if you have a application requirement , user has attended recent events , before querying all the events attended/registered by user then apply Subset pattern .

Favour you application requirement , while data modeling .

2 Likes

Hello Liel,

Building on @Vivekanandan_Sakthivelu’s answer, you should consider your application requirements when designing your model. In your situation, you could ask how often / how many events and guests) are you creating? Are constantly querying guests lists, or events? Are guests used to attending multiple events over time?

For example, if you create events once in a while, but when it happens guest registrations, checking-ins and reporting are very frequent, you may consider using the Extended Reference Pattern (Building with Patterns: The Extended Reference Pattern | MongoDB Blog), adding only certain, frequent-used data from each Event in the reference embedded on the Guest model.

Hope this helps!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.