问题
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