Database Reference
In-Depth Information
The next configuration step is to enable sharding on a database. This is a prerequi-
site for sharding any collection. Your application's database will be called cloud-docs ,
and so you enable sharding like so:
> sh.enableSharding("cloud-docs")
Like before, you can check the config data to see the change you just made. The con-
fig database holds a collection called databases that contains a list of databases. Each
document specifies the database's primary shard location and whether it's partitioned
(whether sharding is enabled):
> db.getSiblingDB("config").databases.find()
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "cloud-docs", "partitioned" : true, "primary" : "shard-a" }
Now all you need to do is shard the spreadsheets collection. When you shard a col-
lection, you define a shard key. Here you'll use the compound shard key {user-
name: 1, _id: 1} because it's good for distributing data and makes it easy to view
and comprehend chunk ranges:
> sh.shardCollection("cloud-docs.spreadsheets", {username: 1, _id: 1})
Again, you can verify the configuration by checking the config database for sharded
collections:
> db.getSiblingDB("config").collections.findOne()
{
"_id" : "cloud-docs.spreadsheets",
"lastmod" : ISODate("1970-01-16T00:50:07.268Z"),
"dropped" : false,
"key" : {
"username" : 1,
"_id" : 1
},
"unique" : false
}
This sharded collection definition may remind you of something; it looks a bit like an
index definition, especially with its unique key. When you shard an empty collection,
MongoDB creates an index corresponding to the shard key on each shard. 9 Verify this
for yourself by connecting directly to a shard and running the getIndexes() method.
Here you connect to your first shard, and the output contains the shard key index, as
expected:
$ mongo arete:30000
> use cloud-docs
> db.spreadsheets.getIndexes()
[
{
"name" : "_id_",
9
If you're sharding an existing collection, you'll have to create an index corresponding to the shard key before
you run the shardcollection command.
Search WWH ::




Custom Search