hash - how do hashed sharding keys work in mongodb? -
i wondered internals hashed sharding keys in mongodb. constant mongodb chooses hash field if timestamp instance ?
guess key timestamp % n ; n ?
if design, guess n cannot change on time. how can sure if have lot of data there not many collisions in 1 shard if n small ?
thanks
a hashed key uses data in document, , standard hash function (md5) generate hash value document. example, suppose index on {name: 'hashed'}
, have these documents:
{name: 'john', height: 73} {name: 'zardosht', height: 68}
mongodb run 'john'
, 'zardosht'
through md5 hash function , random-looking byte sequences. then, it'll use byte sequences actual b-tree key use in index. when want query {name: 'john'}
, md5 hash again, gets same byte sequence have gotten before, , index lookup value.
you can play mongo shell , see these values yourself:
% mongodb-linux-x86_64-2.2.5/bin/mongo mongodb shell version: 2.2.5 connecting to: test > db.runcommand({_hashbsonelement: {name: "john"}}) { "key" : { "name" : "john" }, "seed" : 0, "out" : numberlong("5553133884637223031"), "ok" : 1 } > db.runcommand({_hashbsonelement: {name: "zardosht"}}) { "key" : { "name" : "zardosht" }, "seed" : 0, "out" : numberlong("8641066842148283865"), "ok" : 1 }
note there isn't n
in i've described, don't have worry limits that.
Comments
Post a Comment