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:
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
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.