1

I have added 2 shards to my cluster:

sh.addShard("127.0.0.1:27023")
{
    "shardAdded" : "shard0000",
    "ok" : 1,
    "operationTime" : Timestamp(1552115819, 4),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1552115819, 4),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
mongos> sh.addShard("127.0.0.1:27024")
{
    "shardAdded" : "shard0001",
    "ok" : 1,
    "operationTime" : Timestamp(1552115829, 3),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1552115829, 3),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

I check the status of the shards:

mongos> db.printShardingStatus()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5c83670c4ca86892db695187")
  }
  shards:
        {  "_id" : "shard0000",  "host" : "127.0.0.1:27023",  "state" : 1 }
        {  "_id" : "shard0001",  "host" : "127.0.0.1:27024",  "state" : 1 }
  active mongoses:
        "4.0.6" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

I have enabled sharding:

mongos> sh.enableSharding("testdb")
{
    "ok" : 1,
    "operationTime" : Timestamp(1552115951, 2),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1552115951, 2),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
mongos> sh.shardCollection("testdb.testcollection", {testkey:1})
{
    "collectionsharded" : "testdb.testcollection",
    "collectionUUID" : UUID("d3c87518-98f4-4226-b67f-0d89c2d226b1"),
    "ok" : 1,
    "operationTime" : Timestamp(1552115960, 4),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1552115960, 4),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

I get the database and start adding documents into them:

mongos> db = (new Mongo("localhost:27021")).getDB("testdb")
testdb
mongos> db.testcollection.count()
0
mongos> for (var i=0; i < 10; i++){db.testcollection.insert({"testkey": "key"+i})}
WriteResult({ "nInserted" : 1 })
mongos> db.testcollection.count()
10
mongos> for (var i=0; i < 100000; i++){db.testcollection.insert({"testkey": "key"+i})}
WriteResult({ "nInserted" : 1 })

But after I check the amount of documents in each cluster:

mongos> db.testcollection.count()
136295

There are too many documents. I also checked the number of documents in the 2 shards:

localhost:27023

use testdb
switched to db testdb
> db.testcollection.aggregate([{$count:"nDocs"}])
{ "nDocs" : 100010 }

localhost:27024

> use testdb
switched to db testdb
> db.testcollection.aggregate([{$count:"nDocs"}])
{ "nDocs" : 36285 }

By the looks of it, the sharding is happening:

mongos> db.printShardingStatus()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5c83670c4ca86892db695187")
  }
  shards:
        {  "_id" : "shard0000",  "host" : "127.0.0.1:27023",  "state" : 1 }
        {  "_id" : "shard0001",  "host" : "127.0.0.1:27024",  "state" : 1 }
  active mongoses:
        "4.0.6" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                3 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard0000   1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 
        {  "_id" : "testdb",  "primary" : "shard0000",  "partitioned" : true,  "version" : {  "uuid" : UUID("69bd8a3f-5e6b-42b5-b302-5a7f033e7d8c"),  "lastMod" : 1 } }
                testdb.testcollection
                        shard key: { "testkey" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard0000   4
                                shard0001   3
                        { "testkey" : { "$minKey" : 1 } } -->> { "testkey" : "key0" } on : shard0001 Timestamp(2, 0) 
                        { "testkey" : "key0" } -->> { "testkey" : "key20968" } on : shard0001 Timestamp(3, 0) 
                        { "testkey" : "key20968" } -->> { "testkey" : "key4265" } on : shard0001 Timestamp(4, 0) 
                        { "testkey" : "key4265" } -->> { "testkey" : "key53621" } on : shard0000 Timestamp(3, 2) 
                        { "testkey" : "key53621" } -->> { "testkey" : "key6485" } on : shard0000 Timestamp(3, 3) 
                        { "testkey" : "key6485" } -->> { "testkey" : "key8552" } on : shard0000 Timestamp(3, 4) 
                        { "testkey" : "key8552" } -->> { "testkey" : { "$maxKey" : 1 } } on : shard0000 Timestamp(4, 1) 

Why aren't the nodes adding up to equal to 100,010? Where are the extra ones coming from?

Also, when i add another shard, the number of documents increase? Why is the number of documents increasing beyond the number I added into it?

  • By any chance, your insert process got interrupted in the middle and re-started again? – Mani Mar 9 at 7:55
  • i don’t think so, unless the process is interrupted by itself. during the insertions, i don’t touch anything and just want it until the everything is done? Could the insert process be interrupted without my interaction? – tom dinh Mar 9 at 8:12
  • i haven’t looked at it yet, but I assume it they are duplicates. – tom dinh Mar 9 at 8:16
  • I tried that and all the documents went to 1 shard and the other shard was missing the database – tom dinh Mar 9 at 18:27
  • Let us continue this discussion in chat. – Mani Mar 10 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.