2

Assuming we have an md5 hash:

With ruby:

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

Possible field types:

:type => String

:type => Hash

Which should I choose?

Community
  • 1
  • 1
Lamp
  • 1,084
  • 1
  • 14
  • 27

3 Answers3

7

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

chrisrhoden
  • 464
  • 3
  • 6
4

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.

Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
0

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"))
jqr
  • 654
  • 6
  • 11