Skip to content

MongoDB Sharding-05-Enable Sharding For a Collection

    1. Start Mongo Shell
    [[email protected] tom]# mongo
    MongoDB shell version: 2.6.3
    connecting to: test
    mongos>
    
    2.	List Existing Databases
    mongos> show dbs
    admin	(empty)
    books	0.063GB
    config	0.016GB
    
    3. Chose a Database
    mongos> use books
    switched to db books
    
    4. Create a collection with few documents
    mongos>
    for(i=0;i<=10;i++){db.mybooks.insert({_id:i,name:"book_number_"+i,author:" author"})}
    WriteResult({ "nInserted" : 1 })
    Check the documents
    mongos> db.mybooks.find()
    { "_id" : 0, "name" : "book_number_0", "author" : "author" }
    { "_id" : 1, "name" : "book_number_1", "author" : "author" }
    { "_id" : 2, "name" : "book_number_2", "author" : "author" }
    { "_id" : 3, "name" : "book_number_3", "author" : "author" }
    { "_id" : 4, "name" : "book_number_4", "author" : "author" }
    { "_id" : 5, "name" : "book_number_5", "author" : "author" }
    { "_id" : 6, "name" : "book_number_6", "author" : "author" }
    { "_id" : 7, "name" : "book_number_7", "author" : "author" }
    { "_id" : 8, "name" : "book_number_8", "author" : "author" }
    { "_id" : 9, "name" : "book_number_9", "author" : "author" }
    { "_id" : 10, "name" : "book_number_10", "author" : "author" }
    
    6. List all Collections
    mongos> show collections
    books
    mybooks
    system.indexes
    
    7. Enable Sharding for the database
    mongos> sh.enableSharding("books")
    { "ok" : 1 }
    
    8. Shard the Newly Created Collection
    mongos> sh.shardCollection("books.mybooks",{_id:1},true)
    { "collectionsharded" : "books.mybooks", "ok" : 1 }
    Check Sharding Status
    mongos> sh.status()
    
    9. Sharding Status
    sharding version: {
    "_id" : 1,
    "version" : 4,
    "minCompatibleVersion" : 4,
    "currentVersion" : 5,
    "clusterId" : ObjectId("53bdcc24ecb20b84c96f5914")
    }
    shards:
    { "_id" : "ReplicaSetNew1", "host" : "ReplicaSetNew1/localhost.localdomain:27011,localhost.localdomain:27012,lo calhost.localdomain:27013" }
    { "_id" : "ReplicaSetNew2", "host" : "ReplicaSetNew2/localhost.localdomain:27021,localhost.localdomain:27022,lo calhost.localdomain:27023" }
    { "_id" : "ReplicaSetNew3", "host" : "ReplicaSetNew3/localhost.localdomain:27031,localhost.localdomain:27032,lo calhost.localdomain:27033" }
    
    10.	Insert Few More Documents
    mongos>
    for(i=11;i<=50000;i++){db.mybooks.insert({_id:i,name:"book_number_"+i,auth or:"author",description:"this is a book"})}
    WriteResult({ "nInserted" : 1 })
    
    11.	Check Status
    mongos> db.mybooks.stats()
    {
    "sharded" : true,
    "systemFlags" : 1,
    "userFlags" : 1,
    "ns" : "books.mybooks",
    "count" : 50001,
    "numExtents" : 12,
    "size" : 5084336,
    "storageSize" : 13983744,
    "totalIndexSize" : 1422624,
    "indexSizes" : {
    "_id_" : 1422624
    },
    "avgObjSize" : 101.68468630627387,
    "nindexes" : 1,
    "nchunks" : 4,
    "shards" : {
    "ReplicaSetNew1" : {
    "ns" : "books.mybooks",
    "count" : 9613,
    "size" : 560880,
    "avgObjSize" : 58,
    "storageSize" : 2793472,
    "numExtents" : 5,
    "nindexes" : 1,
    "lastExtentSize" : 2097152,
    "paddingFactor" : 1,
    "systemFlags" : 1,
    "userFlags" : 1,
    "totalIndexSize" : 277984,
    "indexSizes" : {
    "_id_" : 277984
    },
    "ok" : 1
    },
    "ReplicaSetNew2" : {
    "ns" : "books.mybooks",
    "count" : 0,
    "size" : 0,
    "storageSize" : 8192,
    "numExtents" : 1,
    "nindexes" : 1,
    "lastExtentSize" : 8192,
    "paddingFactor" : 1,
    "systemFlags" : 1,
    "userFlags" : 1,
    "totalIndexSize" : 8176,
    "indexSizes" : {
    "_id_" : 8176
    },
    "ok" : 1
    },
    "ReplicaSetNew3" : {
    "ns" : "books.mybooks",
    "count" : 40388,
    "size" : 4523456,
    "avgObjSize" : 112,
    "storageSize" : 11182080,
    "numExtents" : 6,
    "nindexes" : 1,
    "lastExtentSize" : 8388608,
    "paddingFactor" : 1,
    "systemFlags" : 1,
    "userFlags" : 1,
    "totalIndexSize" : 1136464,
    "indexSizes" : {
    "_id_" : 1136464
    },
    "ok" : 1
    }
    },
    "ok" : 1
    }

    Also See:

    1 thought on “MongoDB Sharding-05-Enable Sharding For a Collection”

    1. Scott

      Thanks …for the tutorial

    Leave a Reply