2

I am developing an application where I have multiple types of users Admin, Trainer, Trainee who have few fields in common and few fields specific to the user.

Ex:

Trainer{
    Name: string,
    Email: string, 
    Mobile: string,
    ExpertIn: string[],
    Rating: number,
    ...
}

Trainee{
    Name: string,
    Email: string, 
    Mobile: string,
    Coureses:string[],
    AverageScore: number
    ...
}

Is it good to store all types of users in single collection or keep common fields in users collection and specific details in different collections like (Trainer, Trainee) in terms of MongoDB 4.0 (keeping transactions and joins in mind)?

2
  • What do you think would be the advantage of separating them, or the disadvantage of keeping them together? Commented Sep 19, 2019 at 5:05
  • You can have single User model with type attribute. For courses, you can have Courses model which will have userId or ownerId, which will be userId which is trainer. Similarly for other models. You can also have type model. Which will be property on user model. Commented Sep 19, 2019 at 11:53

1 Answer 1

1

Since users can have n numbers of different characteristics, storing the same in separate collection will be the good approach as storing them in one collection will result in complex schema and one may end up with messing around complex queries on it.

Let me take you to an example i have worked on. I have the almost same scenario where i have multiple users with different positions/roles/authorities. My architecture was like:

  1. users: Collection to store common fields also contains their roleId(reference to _id in roles collection)

  2. roles: Collection which has the roles each type of user has

Now considering the second point of question, mongoose provide populate() to work with two or more collections at the same. Also you can do aggregate queries in case populate doesn't solve your problem.

You can read more about it here:

populate

aggregation

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.