Document design in MongoDB for the given case

what is the best document structure for the below case, I have RDBMS table structure.
|Gender | Age Min (>=) | Age Max (<) | Income Min (>=) | Income Max (<) | Classification |
| Male | 18 | 22 | 1 | 5000 | A |
| Male | 18 | 22 | 5000 | 7000 | B |
| Male | 18 | 22 | 7000 | 99000 | C |
| Male | 22 | 45 | 1 | 7500 | A |

The purpose of table is to fetch Classification by providing Gender, Age and Income of person
Please help

Hey @Ravikiran_Chikkamath,

Welcome to the MongoDB Community Forums! :leaves:

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.

Based on what you described, one example design you can document your data is in the following manner (please test and alter according to your use case and requirements):

 Gender: "Male",
 maxAge: 22,
 classification: "A"


This way, you can search classification by providing Gender, Age, and income. Adding index to these fields would help improve query performance as well. Do read about the ESR rule as well since that will help you a lot while planning your index and queries. I would suggest you use mgeneratejs to create sample documents quickly in any number, so the design can be tested easily.

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


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