No need to do the ensureIndex in php. When sharding your collection, the index gets created automatically. Mostly I try to create the index before the shardCollection, just a habit.
First make sure you don't have Vision_1282 databases on your shards themselves:
shardA:PRIMARY> show dbs
local 6.075GB
shardA:PRIMARY> exit
bye
shardB:PRIMARY> show dbs
local 6.075GB
shardB:PRIMARY> exit
bye
Then I did the following steps:
(using a great script from Adam Comerford to check size/count/place of chunks : https://github.com/comerford/mongodb-scripts)
$ mongo --shell allChunkInfo.js
MongoDB shell version: 3.0.3
connecting to: test
type "help" for help
mongos> show dbs
admin (empty)
config 0.016GB
mongos> sh.enableSharding("Vision_1282")
{ "ok" : 1 }
mongos> use Vision_1282
switched to db Vision_1282
mongos> db.Group.createIndex({groepNr : 1, valId : 1})
{
"raw" : {
"shardB/mongo2-2-1:27018,mongo2-2-2:27018" : {
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1437540334, 5),
"electionId" : ObjectId("55a72087bc8dee49cc03fce5")
}
}
},
"ok" : 1
}
mongos> use admin
switched to db admin
mongos> db.runCommand({ shardcollection : "Vision_1282.Group", key : {groepNr : 1, valId : 1}})
{ "collectionsharded" : "Vision_1282.Group", "ok" : 1 }
mongos> use Vision_1282
switched to db Vision_1282
mongos> db.Group.insert({groepNr:1, valId:3})
WriteResult({ "nInserted" : 1 })
mongos> db.Group.find()
{ "_id" : ObjectId("55af205a0eb443db0213e359"), "groepNr" : 1, "valId" : 3 }
mongos> AllChunkInfo("Vision_1282.Group")
ChunkID,Shard,ChunkSize,ObjectsInChunk
Vision_1282.Group-groepNr_MinKeyvalId_MinKey,shardB,112,1
***********Summary Chunk Information***********
Total Chunks: 1
Average Chunk Size (bytes): 112
Empty Chunks: 0
Average Chunk Size (non-empty): 112
mongos> db.Group.remove({})
WriteResult({ "nRemoved" : 1 })
mongos> db.Group.count()
0
Using a python script to import 10.000 records, then the result was indeed 9999 records on 1 shard and 1 document in the other.
mongos> AllChunkInfo("Vision_1282.Group")
ChunkID,Shard,ChunkSize,ObjectsInChunk
Vision_1282.Group-groepNr_MinKeyvalId_MinKey,shardA,48,1
Vision_1282.Group-groepNr_1valId_2,shardB,360128,7502
Vision_1282.Group-groepNr_4valId_5,shardB,119872,2497
***********Summary Chunk Information***********
Total Chunks: 3
Average Chunk Size (bytes): 160016
Empty Chunks: 0
Average Chunk Size (non-empty): 160016
But... after inserting bigger records with more data... and adding a lot more records ... and patience ... finally you can see the sharding in action:
mongos> AllChunkInfo("Vision_1282.Group")
ChunkID,Shard,ChunkSize,ObjectsInChunk
Vision_1282.Group-groepNr_MinKeyvalId_MinKey,shardA,576,8
Vision_1282.Group-groepNr_1valId_2,shardA,10490992,106806
Vision_1282.Group-groepNr_2valId_4455,shardB,11521744,110015
Vision_1282.Group-groepNr_3valId_14462,shardB,4757472,42486
Vision_1282.Group-groepNr_4valId_5,shardB,8998288,87476
***********Summary Chunk Information***********
Total Chunks: 5
Average Chunk Size (bytes): 7153814.4
Empty Chunks: 0
Average Chunk Size (non-empty): 7153814.4
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("55a754a9ac01a84a90b137cf")
}
shards:
{ "_id" : "shardA", "host" : "shardA/mongo1:27018,mongo2:27018" }
{ "_id" : "shardB", "host" : "shardB/mongo3:27018,mongo4:27018" }
balancer:
Currently enabled: yes
Currently running: yes
Balancer lock taken at Wed Jul 22 2015 05:15:40 GMT+0000 (UTC) by ac2-1:27017:1437029544:1804289383:Balancer:1681692777
Failed balancer rounds in last 5 attempts: 0
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "Vision_1282", "partitioned" : true, "primary" : "shardB" }
Vision_1282.Group
shard key: { "groepNr" : 1, "valId" : 1 }
chunks:
shardA 2
shardB 3
{ "groepNr" : { "$minKey" : 1 }, "valId" : { "$minKey" : 1 } } -->> { "groepNr" : 1, "valId" : 2 } on : shardA Timestamp(2, 0)
{ "groepNr" : 1, "valId" : 2 } -->> { "groepNr" : 2, "valId" : 4455 } on : shardA Timestamp(3, 0)
{ "groepNr" : 2, "valId" : 4455 } -->> { "groepNr" : 3, "valId" : 14462 } on : shardB Timestamp(3, 1)
{ "groepNr" : 3, "valId" : 14462 } -->> { "groepNr" : 4, "valId" : 5 } on : shardB Timestamp(2, 4)
{ "groepNr" : 4, "valId" : 5 } -->> { "groepNr" : { "$maxKey" : 1 }, "valId" : { "$maxKey" : 1 } } on : shardB Timestamp(1, 3)
Conclusion: because the records were too small, and the chunk was not big enough to split yet.
To see the configuration of the size of the chunks:
mongos> use config
switched to db config
mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 64 }