I want to create a user collection with role field

Hi currently i am working on a personal project where i want to implement mongodb, i am trying to create a collection with a document which has a role field (customer, seller, administrator) but all these tree users have extra fields that make them different so i am trying to know what should i do maybe creating 3 different collections? but if that so i am also having an issue where i have a collection that has movement of the items sold in which it has the customer and the seller so i dont know what should i do i can do

I just need some help please

Please be a little bit more patient.

Most of us are not in the same timezone as you.

Most of us are voluntary and are not here full time because we are here during lunch breaks, before work or after work.

With MongoDB and its flexible schema, you can certainly have all your users within the same collection despite the fact that they have different fields. With MongoDB you have the choice. You could created 3 different collections. My preference is to start with 1 collection for simplicity. Most likely I would implement the attribute pattern for the extra fields.

1 Like

ok i understand sorry.

But as i read the blog that attribute pattern is more likely to use to sort data or i didnt understand?, and how about if i implement the Polymorphic Patterns

Very good choice. More appropriate in certain situations.

As for the attribute pattern, for sorting, for indexing and searching. It is more or less finer-grain polymorphic. For example, an inventory: metal, color, size, dimensions, textile could be attributes, so items of the inventory can have some of the attributes. Shoes will have color=Red, size=46, textile=leather. A wrench will have metal=vanadium and size=12, …

I find, polymorphic easier to implement (no array manipulations) but it is less flexible.

You can have a mix of the two and that would be legitimate.

1 Like

Sorry i have an extra question maybe in my users how should i validate the roles and use the fields that should use i cant figure that out i am still learning, the blog does not specify how to differentiate this, like maybe the logic would be role: “customer”, and besides the common fields like name, how should i change the pre schema to use only the fields that i want and not all that it has

I do not know what is the pre schema?

Yes you really need a field that specify which type of data that is stored in the given document.

You can put extra fields in a sub-document. It could be named extra like

  role : "customer" ,
  name : "steevej" ,
  extra : {
    credit_card : "xxxx xxxx xxxx" ,
    interest : [ "mongodb" , "sailing" ]
  role : "seller" ,
  name : "jurgen_r_b" ,
  extra : {
    products : [ "shoes" , "phone" , ... ]

sorry i say as pre schema as predefined, but like maybe i put a new user with role: “customer”, that subdocument i just pass maybe the credit_car and interest field but what about the others field if i dont pass the other fields are they gone be null?

If using native mongo, the extra fields will not exist if you do not specify it.

If using an abstraction layer like mongoose, I do not know.