Is there a way to access the shard resolution algorithm in mongo? I know about db.printShardingStatus(), but that requires me to write my own logic to bucket the key. I’m hoping for something like:
db.collection.runCommand( { getShardsvr : "my-key" } );
> {"ok" : 1 , "shardsvr" : "hostname"}
I’m guessing this is what mongos does when it routes.
For context, I’m streaming a very large amount of data into mongo. There are data collectors using EmMachine to stream data to pre-processor machines which do some data cleansing then insert into a local mongod. What I’d like to do is for each piece of data, the collector will follow the sharding strategy and stream it to the corresponding pre-processor machine. Triggers would accomplish the same thing but mongo doesn’t have them AFAIK.
So the short answer is yes it’s possible, however I don’t know that you will find the command you’re looking for.
MongoDB sharding is pretty simplistic. The config databases contain key ranges and those key ranges point to the correct host. The config databases are actually just regular DBs and you can connect to these directly from a shell or driver. (they typically run on a different default port to avoid accidental connection).
So you can easily look up the host by simply looking at the available ranges. The logic is no more complex than a
forloop until your key is in range.For details on manually controlling sharding look here. That will provide some insight into what’s going on.
Please note that all of the above advice is “warranty voiding”. The whole concept behind
mongos(router) and the config DBs is to abstract this complexity. By doing what you’re doing, you will have to “look under the hood”.