The Mongo ruby driver seems to get put your results into a hash with strings as keys. Is there a way to tell it to convert the keys into symbols instead?
The Hash class has plenty of useful methods that make it convenient to manipulate any given Hash. As such, Hashes are used a lot in the Rails framework, as they are a convenient way to collect some related values or objects together, and allow each object to have a key or label. Symbols are particularly useful as keys within a Hash.
Hashed indexes compute the hash value of a single field as the index value; this value is used as your shard key. [ 1] MongoDB 4.4 adds support for creating compound indexes with a single hashed field. To create a compound hashed index, specify hashed as the value of any single index key when creating the index.
but this class is intended for use cases where strings or symbols are the expected keys and it is convenient to understand both as the same. For example the params hash in Ruby on Rails. Note that core extensions define Hash#with_indifferent_access:
Both Strings and Symbols are useful as keys in a Hash because they look like a label. They help describe the purpose of the key that they are being used for. For example, if we want to store a name and location in a Hash, we can use the Strings 'name' and 'location' to store the values and then look up the values.
According to the FAQ, Even though there is a symbol type in Mongo, and you can store symbols in values, the BSON format specifies that keys must be strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With