[SERVER-32889] Mongo shell injection function for hashing shard key Created: 25/Jan/18  Updated: 30/Oct/23  Resolved: 23/Apr/18

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 3.7.6

Type: Improvement Priority: Minor - P4
Reporter: Dmitry Ryabtsev Assignee: Benjamin Caimano (Inactive)
Resolution: Fixed Votes: 1
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
is documented by DOCS-11656 Document convertShardKeyToHashed func... Closed
Related
is related to DOCS-11970 The convertShardKeyToHashed usage des... Closed
Backwards Compatibility: Fully Compatible
Sprint: Platforms 2018-04-09, Platforms 2018-04-23, Platforms 2018-05-07
Participants:

 Description   

Users may sometimes want to figure out the hashed value for a certain document's shard key field. For instance the reason for doing that might be to identify the shard key range / chunk the document belongs to (when a hashed shard key is used). There a ways to do that, namely:

  • Run a query on the shard key and inspect the index key (the `$returnKey` query option)
  • Run a query on the shard key and inspect the index scan bounds in the $explain output
  • Re-start the server in test mode and use the _hashBSONElement command
  • Port BSONElementHasher into a custom application

All of these methods are pretty clumsy and inefficient when all you need is to get a hashed value for an arbitrary field. It would be much user-friendly if BSONElementHasher could be run on the client side (i.e. in the mongo shell) via a helper function.



 Comments   
Comment by Dmitry Ryabtsev [ 16/Aug/18 ]

Hi ben.caimano, apologies for the late reply. I'm pretty happy with this implementation, granted that the point raised by Kev gets addressed (DOCS-11970). 

Thank you!

Comment by Kevin Pulo [ 16/Aug/18 ]

Thanks ben.caimano, I just used this for the first time and it's very useful!

One thing I noticed, though, is that convertShardKeyToHashed() takes just the value to be hashed. So in the typical usage of a hashed shard key, you just want to do convertShardKeyToHashed(Value), ie. without the ShardKey field prefix.

> db.hashed.createIndex({_id:"hashed"})
{
        "createdCollectionAutomatically" : true,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.hashed.insert({_id:1})
WriteResult({ "nInserted" : 1 })
> db.hashed.find()
{ "_id" : 1 }
> db.hashed.find().hint({_id:"hashed"}).returnKey()
{ "_id" : NumberLong("5902408780260971510") }
> convertShardKeyToHashed({_id:1})
NumberLong("-5167005222132680029")
> convertShardKeyToHashed(1)
NumberLong("5902408780260971510")
> convertShardKeyToHashed({ShardKey:1})
NumberLong("8248593335112860164")
> db.hashed.insert({_id:{_id:1}})
WriteResult({ "nInserted" : 1 })
> db.hashed.insert({_id:{ShardKey:1}})
WriteResult({ "nInserted" : 1 })
> db.hashed.find()
{ "_id" : 1 }
{ "_id" : { "_id" : 1 } }
{ "_id" : { "ShardKey" : 1 } }
> db.hashed.find().hint({_id:"hashed"}).returnKey()
{ "_id" : NumberLong("-5167005222132680029") }
{ "_id" : NumberLong("5902408780260971510") }
{ "_id" : NumberLong("8248593335112860164") }

Comment by Benjamin Caimano (Inactive) [ 23/Apr/18 ]

I've added the following function as a shell injection:

convertShardKeyToHashed({ ShardKey: Value })

dmitry.ryabtsev, does this cover your bases?

Comment by Githook User [ 23/Apr/18 ]

Author:

{'email': 'ben.caimano@10gen.com', 'name': 'Ben Caimano'}

Message: SERVER-32889 Mongo shell injection function for hashing shard key
Branch: master
https://github.com/mongodb/mongo/commit/1f1f0a934d0f79318a2525f1c590ba77959ee2e5

Generated at Thu Feb 08 04:31:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.