Mongodb – Does shardCollection acquire lock in mongo db

mongodbsharding

We are planning to shard a collection that contains few hundred gigs of data. I am going to use sh.shardCollection api for this. This is supposed to take hours to complete. I couldn't find any reference in the docs explaining whether the command acquires lock on the collection or not.

In short, all I want to know is whether collection is going to be available for read and write during while shardCollection is executing or not.

Best Answer

Yes, the collection is available during the process. Actually, that command sh.shardCollection will take only milliseconds to execute, but actual balancing work will take hours or days to be ready. Balancing will not lock collection, it's just moving chunks from one shard to other one.

Just be careful when selecting sharding key, you should have good knowledge of basics of sharding and how it works.