** I asked this question at StackOverflow the other day but am not getting any useful responses. Was hoping someone in this community may be better suited to help! **
Goal: Build a MongoDB schema (in terms of performance and scalability) that can account for several similar booleans. All booleans would be in the same collection, which will have other data as well.
Example (not real, just for discussion): App will have boolean values in collection based on cities someone has lived in over their lifetime. Currently, the app only has 5 booleans (isNYC, isChicago, isSF, isSeattle, and isAustin). As the app increases its users and markets, it will eventually be adding several more cities (let’s say this is capped at 100 different cities in the US). While users will be able to toggle between true and false, the expectation is that the boolean value will not change too frequently.
Ideas Explored:
- Every city boolean is its own object in the collection. As new cities are added, the collection is updated to include a new boolean for that city.
- Attribute pattern with a key value pair for the city booleans. Key is the city and value is true/false. I can have all the cities as T/F or just keys for the cities someone has lived in to keep the size down.
- Array of the cities the user has lived in (e.g. [“NYC”, “SF”, “Seattle”]). As new cities are available the user can add the new city to the array. ** I know this isn’t a boolean, but just wanted to be thorough.
- Something else?!?!
Reactions to ideas Explored:
- Booleans take up very little space in the collection which can enhance performance. This setup is very simple and straight forward on the backend. Initial concern is that on the frontend I may need an if statement to account for every city in case I want to display different information for each city. Once I have 100 cities I feel like my code will get a little messy / jumbled.
- Attribute pattern is great for indexing. I can easily query for the specific city I want but this may be slower than just running a query on idea #1. I like how organized this pattern is but have not seen any use cases of booleans in the attribute pattern but also don’t see why I couldn’t use this approach.
- Likely not the best option. Saving strings takes up more space and then I would have to map through the array and then add an if statement to set up the frontend properly.
Final Note: I am really hoping to find a solid design that allows for the user to select the cities they have lived in, allow the user to add data only specific to that city, and allow other users to find users by city. There may be other purposes as well so something flexible is really important.
Thanks so much!