简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM