How to design schema for different kinds of users?

Hi,

I have two types of users, user A who are basically customer and user B who would be serving the As.

I am wondering if i should have different collections to store this data or have it in the same collection?

Hey @Punit_Pal,

Welcome to the MongoDB Community Forums! :leaves:

Without knowing the nature of the relationship between A and B(one-to-many, one-to-one, or many-to-many), as well as your common queries, it is very hard to give you a definitive answer. A general rule of thumb while doing schema design in MongoDB is that you should design your database in a way that the most common queries can be satisfied by querying a single collection, even when this means that you will have some redundancy in your database. Thus, it may be beneficial to work from the required queries first, making it as simple as possible, and let the schema design follow the query pattern.

I would suggest you experiment with multiple schema design ideas. You can use mgeneratejs to create sample documents quickly in any number, so the design can be tested easily.

In general, favor denormalization when:

  • You have small subdocuments
  • Your data does not change very frequently
  • Your documents grow by a small amount over time
  • You often need to query this data
  • You want relatively faster reads

and favor normalization when:

  • You have large subdocuments
  • Your data changes very frequently
  • Your documents grow by a large amount over time
  • Your data is often excluded from your results
  • You want relatively faster writes

Do note that these points are just general ideas and not strict rules. I’m sure there are exceptions and counter examples to any of the points above, but generally, it’s more about designing the schema according to what will suit the use case best (i.e. how the data will be queried and/or updated), and not how the data is stored (unlike in most tabular databases where 3NF is considered “best” for most things).

You can further read the following documentation to cement your knowledge of Schema Design in MongoDB.
Data Model Design
Factors to consider when data modeling in MongoDB

Hope this helps. Feel free to reach out for anything else as well.

Regards,
Satyam

1 Like

They can have Many-to-Many relationship

1 Like

Hey @Punit_Pal,

Thanks for letting me know. The above pointers should help you out a lot while modeling your many-to-many relationship. I’m also linking some conversations from the forums that you might find useful:

  1. Schema design: Many-to-many relationships and normalization
  2. Modelling N-to-N relationship with one document per relationship

Hope this helps too! :smile:

Regards,
Satyam

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.