MongoDB sharding multiple collections with the same shard key

mongodbsharding

Lets imagine I have two mongodb sharded collection: likes & comments.

Both collections are sharded, both collections have userId as a sharded key.

Lets imagine that likes collection is huge and comments collection is small. I have 10 shards in total, let's name them shard A to shard G.

Can I assume that if some particular user's likes are in shard A, then his comments will be in shard A as well?

So what I want to achieve is to have any particular user's data in the same shard. Other user's likes may be in shard C, then his comments should be in shard C as well.

Is it possibly to configure MongoDB? ( or it's the way how it's working by default? )

Best Answer

Is it possibly to configure MongoDB? ( or it's the way how it's working by default? )

Yes, if you want to control data location, you have to use Zones. Follow this link for a better explanation on how to do it: https://docs.mongodb.com/manual/tutorial/sharding-segmenting-shards/ .