Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

Collection set up MongoDB scaling

I'm trying to conceptualise the best way to scale a MongoDB Database.

For example, say you want to create a site selling houses in different states using a MongoDB on the backend. For future scalability and site performance, would it be best practice to have multiple collections for every state or list everything under USA and then search or find the state within the collection?

**Collection - Texas**

{"_id":{"$oid":"xxxxxxxxxxxxx"},"name":"Modern loft","state":"Texas"}
{"_id":{"$oid":"xxxxxxxxxxxxx"},"name":"Vacant Plot","state":"Texas"}
x 1000 entries

**Collection - USA**

{"_id":{"$oid":"xxxxxxxxxxxxx"},"name":"Family Home","state":"Texas"}
{"_id":{"$oid":"xxxxxxxxxxxxx"},"name":"Beach House","state":"California"}
x 1000 entries
question from:https://stackoverflow.com/questions/65858431/collection-set-up-mongodb-scaling

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It all depends on how would you search and present the data...

From performance perspective even it will be best to split the data in database per state and collections to represent the city , but this mean additional logic in your app to support the model and the logic to fetch some global details from all states/cities will become abit more complex.

If you have everything in single collection and you choose good indexes there will be no issues , at some point in time when the database grow you will just need to split the collection in more shards , but the logic to deal with the database will stay simple since you will have everything in single place. ( this is where your database of choice-mongodb can be of help since the sharding mechanism is build in and very easy to configure )

So to me seems splitting the data to states as good compromise since there is no need the people from Oregon to query all the USA data to find few houses in some small village in Oregon when they can search only the collection in Oregon , but at the same time if you shard the collection and you have good sharding index that include the state , the query will go just to the shard where Oregon data is located ... , with the sharding there is other trick for example if you search houses with swimming pool , but you dont include the state in the filter ( if it is sharding key ) the query will do scatter-gather to all shards to find those hauses ...

I hope that answer your question ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...