i am trying to execute the following command within mongo shell script :
sh.shardCollection('mydb.collection', { shardKey : 1 });
but it does not do anything.
However When i execute from script :
print(sh.help())
I get all the help options.
So means 'sh' variable is also availabe in mongo shell script.
then why i can't execute shardCollection though the mongo shell script?
EDIT : Thanks to neil for his comments.. Here is my mongo shell script
db = connect("mydb");
print(db.help());
var cols = db.getCollectionNames();
var names = [
'a',
'b',
'c'
];
var forEachCol = function(i){
if(i === cols.length) return print('Sharding on collections Succesfull');
if(names.indexOf(cols[i]) !== -1) {
sh.shardCollection(cols[i], { shardKey : 1 });
}
forEachCol(i+1);
};
forEachCol(0);
Each of the documents in each collections a,b,c
have a field 'shardKey' (with index). So 'shardKey' help to keep documents with a specific shardKey on one shard.
now when i go to mongo shell and run
db.printShardingStatus()
I get the output as
{ "_id" : "mydb", "partitioned" : false, "primary" : "shard0000" }
and sharding for no collection being listed here.
Means the sh.shardCollection
not working.