Why is it ok to leave fields blank and why they dont show when i create the object

I have a question. Given my own schema where i have a maybe.
name: String, role: String, a: String, b: String, c: SubDocument
i am creating a new user which has a role, and by it’s role i want to pass different fields.
like name: "A", rol: "admin", c: "{createdSomething: "[a]"}", but i am stuck here just blowing my mind in if it is ok to just not pass the other fields and what is happening to them are they null?, are they just going to be occuping memory?, is it a good practice to do that?. also should i apply here the Polymorphic Pattern?

MongoDB is not a relational database. Fields present in one document in a collection do not have to match in shape other documents in the same collection, nor do fields need to be present at all. You can programmatically associate validation rules with a collection, in which case documents will have to conform, but that is optional.

3 Likes

Thank you so much, so it is not wrong to have different users (different users saying with different roles like seller, customer or admin in one collection right?). And one extra question assuming i apply the validation rules, am I using the polymorphic pattern?

so it is not wrong to have different users (different users saying with different roles like seller, customer or admin) in one collection right?

It’s not “wrong” in the sense that it’s not a violation of the document model of MongoDB. It may or may not be optimal … that’s a design decision.

And one extra question assuming i apply the validation rules , am I using the polymorphic pattern ?

Validation rules enforce your design decisions. Your design may or may not be polymorphic.

2 Likes

And how should i make it optimal? i want to ensure to apply all the best practices sorry, should i divide my users in 3 different collections? like one for admins, sellers and customers? or how thank you so much for the answer

It depends on one’s use case. MongoDB is both more flexible than an RDBMS and at the same time less mature. The design patterns for an RDBMS are more established than currently is the case with MongoDB.

My advice is that whatever you chose, impose validation. Validation can be a measure of sorts: if it is terribly complex to write the validation rules, then probably your design is sub-optimal.

1 Like

Ok thank you so much, one last question by Validation we are refering to the Validation rules?

Yes, validation rules are the mechanism for validation in MongoDB.

1 Like