Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting hash with symbol as keys for mongo in rails

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?

like image 465
etang Avatar asked Feb 25 '11 22:02

etang


People also ask

What is the use of hash class in rails?

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.

How do I create a hashed Index in MongoDB?

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.

What is the purpose of the hash class in Ruby?

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:

What are strings and symbols used for in HashMap?

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.


1 Answers

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.

like image 66
Mark Thomas Avatar answered Sep 23 '22 19:09

Mark Thomas