问题
I am currently working on building database to store country-state-city information which is later to be used from drop down menus in our website.
I wanted to get few suggestions on the schema that I have decided as to how efficiently it will work.
I am using MongoDB to store the data.
The schema that I have designed is as follows:
{
_id: "XXXX",
country_name: "XXXX",
some more fields
state_list:[
{
state_name: "XXXX",
some more fields
city_list:[
{
city_name : "XXXX",
some more fields
},
{
city_name : "XXXX",
some more fields
}
]
}
]
}
The data will be increasing. Also there is a long list of cities for each state.
How good this schema will work for the intended purpose?
Should I use linking documents technique (this will require manual coding to map the _id) ?
回答1:
I would suggest to keep it 1-N on country with respect to state while city to be separate collection with stateid. You will have each country with limited number of states, while state will have many cities. Making cities embedded in state and then states embedded in country will make the country collection too huge. The common notion for making schema design is based on following criteria:
1-Few (in this case embed the collection inside parent object.
1-many (in this case create array of ids inside the parent collection and keep chil collection as seperate.
1- scillions (in this scenario keep the parent id in the child object and keep them as separate collections.
This link explains it much better.
回答2:
I think as your data will increase , this schema will collapse. The best way is to break the database in 3 schemas and use refrence of their Ids.
Country Schema:
{
_id: "XXXX",
country_name: "XXXX",
some more fields
state_list:[{
"_id": reference id to state object
}]
}
State Schema :
{
_id: "XXXX",
state_name: "XXXX",
some more fields
city_list:[{
"_id" : reference to city object
}]
}
City Schema:
{
_id: "XXXX",
city_name: "XXXX",
some more fields
}
来源:https://stackoverflow.com/questions/32197603/storing-country-state-city-in-mongodb