Data structure for list of items grouped country and category

Hello i am new in mongo and BE in general. I would like to create a big list of items that will be grouped by country(whatever) and then also by more categories. Most of the times I will need api that will call something like:

domain.com/france/categoryA
domain.com/france/categoryB
domain.com/germany/categoryA
domain.com/germany/categoryB

I suppose is better to create collections per each country + category. Then when I will have to find get search something will be faster and more efficient. There will not be cases for needing the categoryA of many countries. How are you gonna structure and do this with mongo and what prons and cons you see.
Thank you very much

Hey @jelu_k,

Welcome to the MongoDB Community Forums! :leaves:

A general rule of thumb to follow while designing your schema in MongoDB is that things that are queried together should stay together. I’m solely basing my answer on based on what you described since we don’t know what fields you have in mind, but how about keeping your countries as different databases and then keep the different categories in these countries as the collections in those databases, something like this:

Database 1 (France)
   Collection 1 (category A)
   Collection 2 (category B)
   ...
Database 2 (Germany)
   Collection 1 (category A)
   Collection 2 (category B)
   ...
...

This way, your data will stay more organized, and queries by countries + category will be much easier.

I would like to point out that, there are multiple ways to model data in MongoDB, and there isn’t necessarily a “one-size-fits-all” approach to schema design. It may be beneficial to work from the required queries first and let the schema design follow the query pattern. You can also use mgeneratejs to create sample documents quickly in any number, so the design can be tested easily.

Regarding the search to be faster and more efficient, you can use appropriate Indexes to make your queries faster.

Additionally, since you mentioned you are new to MongoDB, I am suggesting some resources that you can go through to expand your knowledge of MongoDB:
Introduction to MongoDB
Data Modelling Course
Data Model Design

Please let us know if you have any additional questions. Feel free to reach out for anything else as well.

Regards,
Satyam

1 Like

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