Home > other >  related tables in mongodb
related tables in mongodb

Time:04-12

I have two related tables in mongodb. such as users and students. Since each student is also a user, I store the userid as a userid in the students table. Should I do this? What is the best practice in this regard? I'm a little confused as I'm coming from a relational database. enter image description here enter image description here

CodePudding user response:

If you will follow that pattern, will be better if you save the userId in the students collection (not a table) as ObjectId instead of string, because that will allow you to do lookups easily.

Another idea can be save the type attribute in users collection to specify what kind of user a user is. Because as I see your are using exactly the same information in both collections, the only thing that will be different is the context.

  • Related