Best practices for efficiently storing md5 hashes in MongoDB

浪尽此生 提交于 2019-12-11 01:04:42

问题


Assuming we have an md5 hash:

With ruby:

>Digest::MD5.hexdigest("ZZtop")
=> "d3e5c7c22df12b70e882f593432a3bdd"

Possible field types:

:type => String

:type => Hash

Which should I choose?


回答1:


Use a String. A Hash in BSON refers to a key-value pair set.




回答2:


In MongoDB, hash does not mean a cryptographic fingerprint (as in MD5 or SHA-1). It means hash as in hash table (a data structure that allows the storage of key-value pairs).

You have to use a string to store a MD5 fingerprint.




回答3:


String, or better yet is to use Binary, its about half the size.

> Digest::MD5.hexdigest("ZZtop").size
=> 32
> Digest::MD5.digest("ZZtop").size
=> 16

You may have to get around the UTF8 check by explicitly telling stating its BSON::Binary.

> BSON::Binary.new(Digest::MD5.digest("ZZtop"))


来源:https://stackoverflow.com/questions/8991447/best-practices-for-efficiently-storing-md5-hashes-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!