MongoDB Documentation, Release 3.0.4 11.5.4 What happens to unsharded collections in sharded databases? 11.5.5 How does MongoDB distribute data across shards? 11.5.6 What happens if a client updates a document in a chunk during a migration? 11.5.7 What happens to queries if a shard is inaccessible or slow? 11.5.8 How does MongoDB distribute queries among shards? 11.5.9 How does MongoDB sort queries in sharded environments? 764 Chapter 11. Frequently Asked Questions In the current implementation, all databases in a sharded cluster have a “primary shard .” All unsharded collection within that database will reside on the same shard. Sharding must be specifically enabled on a collection. After enabling sharding on the collection, MongoDB will assign various ranges of collection data to the different shards in the cluster. The cluster automatically corrects imbalances between shards by migrating ranges of data from one shard to another. The mongos routes the operation to the “old” shard, where it will succeed immediately. Then the shard mongod in- stances will replicate the modification to the “new” shard before the sharded cluster updates that chunk’s “ownership,” which effectively finalizes the migration process. If a shard is inaccessible or unavailable, queries will return with an error. However, a client may set the partial query bit, which will then return results from all available shards, regardless of whether a given shard is unavailable. If a shard is responding slowly, mongos will merely wait for the shard to return results. Changed in version 2.0. The exact method for distributing queries to shards in a cluster depends on the nature of the query and the configuration of the sharded cluster. Consider a sharded collection, using the shard key user_id , that has last_login and email attributes: If you call the cursor.sort() method on a query in a sharded environment, the mongod for each shard will sort its results, and the mongos merges each shard’s results before returning them to the client. • For a query that selects one or more values for the user_id key: • For a query that selects user_id and also performs a sort: • For queries that select on last_login : mongos determines which shard or shards contains the relevant data, based on the cluster metadata, and directs a query to the required shard or shards, and returns those results to the client. mongos can make a straightforward translation of this operation into a number of queries against the relevant shards, ordered by user_id . When the sorted queries return from all shards, the mongos merges the sorted results and returns the complete result to the client. These queries must run on all shards: mongos must parallelize the query over the shards and perform a merge- sort on the email of the documents found.