Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What algorithm does MongoDB use to hash ObjectIds?

To build an index on MongoDB objectIds, I use db.collection.ensureIndex({ _id: "hashed"}).

This generates a Number type that the collection is indexed on. What is the algorithm that goes into this? Is it MD5? And how would I call it on ObjectID objects?

For example, I would like to do something like pull the top 10 _id hashes from a collection, and use that for processing.


1 Answers

Looking at the file hasher.cpp in the MongoDB sourcecode, the used hash function is indeed MD5.

However, MongoDB only uses these hashes internally. It does not expose them to the public API. A hashed index only allows you to query for exact values. It does so by taking your input, hashing it as it hashed the indexed field in the database, and searching for a collision in the hashtable which represents the index.

like image 182
Philipp Avatar answered Sep 08 '25 16:09

Philipp