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?