I want to create a schema for an application which includes users, events and event tasks

I’m creating an app for which users can register and join different events (by paying specified amount). An event can be subscribed in three different ways i.e

  1. Solo (1 Person)
  2. Duo (2 Persons)
  3. Group (More than 2 persons)

Let’s name this as SubscriptionCategory. There would be different set of tasks in each SubscriptionCategory.

Now to complete event users will have to complete the given tasks. Each task will have some scores which user can gain after completion of each task. At the finishing of event a user or a group which will have maximum scores will be declared as winner and would be eligible for the prize money.

Schema I have been thinking is below:

— DATEBASE NAME ( Social-Events )
— — User ( collection )

{
    name: {
      type: String,
      required: true,
      trim: true,
    },
    email: {
      type: String,
      required: true,
      index: { unique: true },
      lowercase: true,
      trim: true,
    },
    events: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    formattedAddress: String,
    password: {
      type: String,
      required: true,
      trim: true,
      minlength: [6, 'Password must be 6 characters long'],
    },
  }

— — Event ( collection )

{
    name: {
      type: String,
      required: true,
      trim: true,
    },
    startsAt: {
      type: Date,
      required: true,
    },
    endsAt: {
      type: Date,
      required: true,
    },
    prizeMoney: {
      type: Number,
      required: true,
    },
    subscriptionCategory: [
      {
          name: String,
          price: Number,
          tasks: [
             {
                name: String,
                description: String,
                scores: Number,
             },
         ],
      }
    ]
    subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  },

Now the issues I’m facing are:

  1. I’m not able to store how a user subscribed to the event?
  2. The Tasks user have completed in an event
  3. Whether the task user completed have been approved as done by the admin.

Should I suppose to make kind of bridge collection similar to Bridge table we use to make in relational database?

I’m sorry I’m new to NO-SQL and I can’t figure that on my own. I’ve followed basic guideline to model a database from attached link.

Hey @Ali_Waqar,

Welcome to the MongoDB Community Forums! :leaves:

You can do this in your event collection itself. You can add a new field in that collection that specifies how a user subscribed to that event.

For this, I would recommend creating a new collection. In this, you can create references to both user and event collections for which the task was completed, as well as the details of the task completion. This should also allow you to store the tasks that each user has completed for each event, along with whether or not they have been approved by the admin by having a field named approved which can be of type boolean which you can set to true once the task has been approved by the admin.

Additionally, I would like to point out a few things about data modeling in MongoDB. A general thumb rule to follow when designing schema in MongoDB is that things that are queried together should stay together. Thus, it may be beneficial to work from the required queries first, making it as simple as possible, and let the schema design follow the query pattern. You can use mgeneratejs to create sample documents quickly in any number, so the design can be tested easily.

I’m also attaching some more resources that you should find useful:
MongoDB Data Modelling Course
MongoDB Schema Design

Hope this helps. Feel free to reach out for anything else as well.

Regards,
Satyam

Thanks for the help @Satyam. I feel I’m on right track then :smiling_face:

1 Like

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