How to manage a DB with collections with different fields

I am developing an application with Mongodb, with roles. I have a collection called users (with username, password, role, active and _id) and another collection that have each roles (student, teacher and admin) that have it’s reference fields or subdocument.

I did it so, because the relation itself is with other collections (exams, tutors, bugs and so on) is not with the user entity, but also the subtype (student, teacher and admin).

The _id field for the user and the subtype collection is the same. I created a transaction in Mongodb to create a user and the document according to the role that is in the User collection (in collection Students or Teachers or Admins) with the same id which is in the Users collection.

Also, the _id in subtype collection’s is a reference field to the User Collection (is primary key and reference field). I am not sure if this is Ok, i have serious doubts.

Ok. The idea, in the part of Express and Mongo, is that when we access a protected record of the application, we pass a Token with the user ID (in headers). In this way, we can access user and validate if it exists, his role and is the owner of the entity we want to modify (ex: one student shouldn’t modify profile data of other users).

Problems with this:

  • If we want to restrict access to the document in a REST service, we would have to take into account if the IDIt is user, subtype or other entity that relates to the subtype.

  • It becomes strange if we make the populate of the user’s data through the Subtype ID field (it would appear as an ID instead of for example, task and it is not apparently configurable).

  • From the URL it is difficult to see the collection we are accessing, since it is not always so obvious.

To sum up, i am confused building an application that uses roles, and the collections depending on an “abstract user”, because the subtypes have different set of fields, and how me should manage in a real application.

As Front i am using Angular 2+. Could you give me a hand? Thank you,


Please, help me! I posted ten days ago this question and i can’t solve it without your help

1 Like

Hey @Antonio_Ubeda_Montero!

Sorry for the delay! Let’s see if we can help you out. :smiling_face_with_three_hearts: Before we address how to manage related collections of data, as this is totally possible in MongoDB (we will get to this in a moment). However, this approach works great when using an RDBMS, but isn’t recommended when using MongoDB. I wrote more about this on the MongoDB Developer Hub . tl;dr: I would personally recommend embedding the role within the user document, unless you have a good reason to keep it separate, and I am not seeing a reason here (correct me if I’m wrong though :smiling_face_with_three_hearts:). Embedding this data directly is going to save you a lot of effort.

Alright, now that we’ve addressed the schema design, let’s address how to do this with your existing schema design. I want to address the issues in your OG post:

One of the downsides of keeping this data separate is that you will need to make at a minimum two queries to your database to get all the data you need. This isn’t bad, it’s just something that’s true in this case.

This is true, you will need to make a query for this.

This isn’t an issue, since Angular 2 follows the MVC pattern, so your data model should be kept separate from the controller on the frontend anyways. There is no need for the frontend to know what your data model on your backend looks like. So, this shouldn’t be an issue.

To sum up, I think it’s going to be more effective for us to discuss why you are separating our this data and figure out the best data model for you application. Looking forward to hearing from you!

1 Like


First of all, thank you for reply. In Mongodb modeling course says that is best to use sub documents than reference and that all data that needs to query together should be stored together inside the same collection.

But if the number of elements is too big (over 50000) is better keeping separate into another collection. Also because mongo when you query a document gets all document and if it can be stored in RAM must access to the hard disk (that is slower).

This is the reason because i decided to reference rather than using subdcuments (in some cases i use it). The reason for i keep separate User and sub types is for two reasons:

  • In the frontend, when Angular guard query the database to see role, in order to let or forbid the access to private collections, if we collect together when i query the other collections i will come into the logic of my guard.
  • The relations are atached not to user in general, but also a concrete type of User (Student, Teacher or Admin). User give us the role and basic information and subtype give us the rest of information about that user.

These are the reasons. I am stucked with this.

How would be the best way to manage this? Thank you in advance.

1 Like

I replied in a different message. Sorry, is in a comment below

1 Like

Hey Antonio, Sounds good! Can you clarify what kind of help you need to manage your collections? Are you looking for help putting together queries? Are you looking for scalability tips? Are you looking for help integrating it into your front end? Please advise. Thank you!

1 Like

Hi Joe.

The main problem i have now is when doing a signin: Should i use a transaction to insert into the User’s collection and the subtype collection? The id’s of User and Subtypes (student, teacher and admin) should be the same or should be different and refere to the User’s collection and if they are different, in the case of managing a JWT that keep the user’s id how would be the best way to identify if the user has the rights to Insert/Delete/Update one document (for instance, one student shouldn’t modify the data of another student).

In student we have one _id, in subtypes another (or not?) and maybe in another collections another more _id. Ufff! I am new with Mongo and i have overwhelmed :frowning:

And of course, in the Front (Angular) i want to keep independence of User’s guard.

Yeah, the main problem is with my backend in Mongo to connect well with front-end.

1 Like

Thank you for your time.

1 Like

Putting transactions around everything in MongoDB is a bit of a code smell for me. I wouldn’t recommend it in this instance. If you are nervous about it, you can up the write concerns for your DB.

Keep the IDs unique, but be sure to add a new field that tracks the relationship in the user document. Managing UUIDs yourself is a good way to get a collision.

It sounds like your making this a little more difficult than it needs to be. Can I ask what this is for? Is this a student project? Do you anticipate more than 500,000 users on your service? Honestly, if this is an MVP, I would make it easy, and refactor your schema as your app scales and grows. You know what they say, “Premature optimizations is the root of all evil.”


It’s a portfolio to show to companies, in order to hire me. I am a Cobol programmer (yes, prehistoric :slight_smile: ) that since a few years ago lost his jobs and bet to transform to Web technologies. But the job market in Spain is too hard: for a Junior profile they ask 2 years of experience and to know a lot of technologies.

The design is thought on the principles of SOLID and design patterns. I like to mantain this as open as i can for, in the future be more scalable.

But you are all right. Doing transactions is not good… I am learning by myself and i can’t know anybody to have a guide of how it is developed in real life proyects.

How would you do?


That’s so cool! That’s a great idea! If this is just for a personal portfolio site, designing it for a massively scalable schema is probably overkill for your use case. That’s great you’re following SOLID priniciples, but it’s important to know how to denormalize your data in order to take advantage of MongoDB documents and it’s features. It’s hard to unlearn RDBMS schema design best practices. This is very normal, so don’t stress about it. I would recommend checking our the MongoDB Developer Hub or check out a couple of course at Those both are a great place to start :slight_smile: I would also recommend checking out these courses:

Another great option is to check out Angular projects that use MongoDB to see how others integrate it and model their data. But everyone learns differently :smiling_face_with_three_hearts:

Does that help? Anything else I can help you with?


I have already taken M320. I’ll try taking M220JS, but i did two courses of Angular (one introduction and another with MEAN with UDemy but the cases didn’t match the logic in this case). I am nowadays without job and no incomes… It’s difficult for me be patient.

Thank you anyway. I will keep searching a solution to this design problem.

1 Like

We can talk about it here too! Can you post your schema here so we can look at it more? I want to see the structure.

1 Like

I but a snippet and general logic. Could i send you it in private? I don’t feel confortable publishing all my schema in public because it is a real project.
Thank you.

1 Like

You can, but I would very much prefer to do this in public, that way everyone can benefit :smiling_face_with_three_hearts: If you have any fields you want to keep private, just remove them or alter them and put in fake data if you need to. Thank you!


I would prefer in private. People knows enough to have a idea of the problem, and later we can give a solution and a brief description of the problem, with more data if you can. How can i send you the Schema? Thanks in advance

1 Like

You can DM me on here :slight_smile:

Sure. I have sent you a message today. Thank you.

1 Like

Hi @JoeKarlsson ! I sent you eleven days ago the schema. Could you help me, please?
I did all the courses you recommend me, but none of them could help me with that.
Thank you,

1 Like

Please, can anybody help me? This doubt is waiting since more than one month and my project is stopped. I did all the recomendations, take the courses recommended and send my collection’s schema…

1 Like