JohnPortella
JohnPortella

Reputation: 1821

Design mongodb schema for inheritance projects

I have a problem in my project.Relationally, I would have the following tables:

Users

nickname passwd session

** Student, Teacher and Managerial inherit Person ** ** A person is a user **

Person

name lastName DNI

Student

codStud semester

Teacher

codTeach type specialty

Managerial

codManag section type charge

Should I embed all the tables and put them together in one collection?

Thank.

Upvotes: 0

Views: 690

Answers (2)

Morteza Adi
Morteza Adi

Reputation: 2473

this is not a decision you could make just by your search criteria!! simple answer to your question is:

it depends!

you have all(almost all) restrictions of Object Relational Mappings in MongoDB when you are storing an object in it.

generally there are 3 approaches here!

First:

you can store each Object in its own collection. for searching, you can simply call each one or all tree together!

Second: you can separate child's and parents in separate collections and reference them by Id

{"name":"Steph", "semester":2, "personID":123}

this is not a best approach in mongoDB as its a document base DB and doesn't support transaction across 2 collections! but may be helpful if the size of object grows beyond 16MB.

the third approach is what @Pangea mentioned:

Storing all object in one collection with distinction key!.

{"type":"Student", "name":"Steph", "semester":2}
{"type":"Teacher", "name":"George", "Courses":[101,202,303,404]}

this has a big drawback you need to know before making decision.

beside normalization which is not much concern in MongoDB, size and maintenance of the collection can be an issue!

however the major drawback is by doing this you are mixing your Objects Specification in one collection, in consequence you can not convey with all these specification. let me explain more: suppose you have unique name for teacher but students allowed to have duplicate names. so you whether have to make name field in collection unique so student have to has unique name which has contradict with Student specifications. or don't make name field unique so it has contradict with teacher Specification!

which of these three approaches have its own benefits and drawbacks. but generally speaking i will go for first approach! because its is simpler to understand, maintain, and scale. however it is your software! you for sure better know your software requirements and specifications. so you are the only person can judge which one is better!

Upvotes: 0

Aravind Yarram
Aravind Yarram

Reputation: 80176

When you use a document store like MongoDB, your schema design is influenced heavily by the access patterns or how the data is used by the user. It is kind of pre-joining/aggregating of the data.

EDIT

Since you require a general search, I suggest to store in a single collection with a type attribute. So to search for all students of 2nd semester you would use the below query

db.users.find({"type":"student", "semester":2})

Upvotes: 1

Related Questions