// Make sure the database can be sharded. sh.enableSharding(dbname); // Shard the collection on the { _id : 1 } key. sh.shardCollection(fullName, {"_id" : 1 }, true) // Also consider sharding on the index { _id : "hashed" }. // That will work well for all YCSB operations except a scan // and you can then skip the rest of this script since you can // ask MongoDB to create and balance the empty collection with a // command like: // sh.shardCollection(fullName, // { "_id" : "hashed" }, // false, // { numInitialChunks : shardDocs.length * 10 }); // Figure out the names of the shards. var shards=[]; for ( var shardDocIndex in shardDocs ) { var shardDoc = shardDocs[shardDocIndex]; if( shardDoc.draining ) { print("Not including the draining shard " + shardDoc._id + ".\n"); } else { shards.push(shardDoc._id); } } print("Splitting " + fullName + " across shards:\n"); for ( var index in shards ) { print(" " + shards[index] + "\n"); } // Split the collection and move chunks around. var shardIndex = 0; for ( var i in digits ) { for ( var j in digits ) { var splitLocation = prefix + digits[i] + digits[j]; var destination = shards[ shardIndex % shards.length ]; shardIndex += 1; print("Splitting at " + splitLocation + "..."); sh.splitAt( fullName, { "_id" : splitLocation } ); print(" moving to " + destination + "..."); sh.moveChunk( fullName, { "_id" : splitLocation }, destination ); print(" done."); } } // Start the balancer again. sh.startBalancer();