Reputation: 317
In mongodb, i want to use mongos and do mongodb sharding over 2 machines, is it common to have a single collection and adding an documents to my collections such as :
{type:'user',name:'xxx',id:1,.........}
{type:'userentery',userid:1.........}
{type:'usersettings',userid:1.......}
{type:'userevent',userid:1.......}
{type:'SomthingNotRelated',....}
Upvotes: 0
Views: 4632
Reputation: 2256
Sharding is the ability of Mongo to split a single collection (any collection) in shards (pieces of the collection) into different small databases (to make it simple). For you it's completely transparent, you use a collection "colX" sharded and you can split it into several machines if you want. The only recommendation is you have to be smart enough and read the documentation to use a proper shard key that helps you to split in the best balanced way possible your collection. You can use your collection and in case this type is statistically relevant to represent a good balance in the collection (it means if you have 10 million records and 10 types it's normal you have around 1 million each) you can use it and shard by type.
Your approach is correct, you just need to use the correct shard key.
One more comment added to my note. A wrong shard key won't accelerate your process too much, if you query by type and your shard key is type it's faster to get the proper shard to return your information. In the other case, if you need, let's say to query by date and it's not in your shard key, Mongo will need to send your query to every shard and merge the result at the end. Sharding help you a lot in some cases and not too much in some other cases, of course you duplicate processor and it's always better but you won't see a big difference always if you didn't choose your shard key properly.
Upvotes: 1
Reputation: 43884
"shredding" no such word for MongoDB. It is "sharding", since you cannot get the name right I would strongly suggest you read the documentation right here: http://docs.mongodb.org/manual/core/sharding/
is my understanding correct to how you should use mongodb?
From what I understand yes.
and is the the way to do horizontal scaling and avoid vertical scaling by avoiding adding more collections?
More or less. Sometimes an aggregate collection of another, containing unique entries or summed entries is also helpful for scaling.
what are the disadvantages of my approach?
You haven't really described a specific approach to anything as such I cannot answer that.
if a user had a very big array wouldn't it be better putting it in a seperate document rather than the user document itself?
Depends on the operations of that array. If the array were to be consistently and continiously updated so that it would dramatically shift in size regularly then yes, you would be better splitting it off.
Such subdocuments are normally actually separate entities in themselves when thought of logically.
Upvotes: 2