shardCollection in ruby

Is there a way to run shardCollection using the ruby driver? I did a listCommands and the closest thing seems to be an internal command:

   "_configsvrShardCollection"=>
    {"help"=>
      "Internal command, which is exported by the sharding config server. Do not call directly. Shards a collection. Requires key. Optional unique. Sharding must already be enabled for the database",
     "requiresAuth"=>true,
     "slaveOk"=>false,
     "adminOnly"=>true},

The shardCollection command can be run via the Ruby driver directly. Note that if sharding hasn’t been enabled for the database yet you’d need to run enableSharding first.

Both commands above are admin commands, so they’d need to be run against the admin database. For example:

require 'mongo'
# connect to local cluster's test database
client = Mongo::Client.new('mongodb://localhost:27017/test')
# connect to local cluster's admin database
admin = client.use(:admin)
# enable sharding of the "test" database
admin.database.command(enableSharding: "test")
# create an index on test.foo
client[:foo].indexes.create_one(_id: "hashed")
# shard the test.foo collection on the index we just created
admin.database.command(shardCollection: "test.foo", key: { _id: "hashed" })

Oh I wasn’t using the admin db. Thanks, I’ll try this out

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.