dimas priyanto - a guy with mouse and keyboard

querying hash field in mongoid

Written on June 11, 2011

MongoDB in general and Mongoid in specific of Rails use, has an awesome Hash data type, it’s like JSON on Javascript and Hash in Ruby.  In traditional database system or relational database which only possible to save Number, String or Datetime, this is not possible to store a Hash data type, so we will need to do more database normalization and more complex database schema in result.

By using MongoDB, we can save a key-value data on a field which contains many data or collection of data. Let’s say we have a model like the following snippets:

class Computer
include Mongoid::Document
field :specification, :type => Hash
end

We can do a querying until the field value keys, in case of Hash field.

Computer.where(’specification.webcam’ => ‘1.3MP’)

Is that nice?
Here is for more information: http://mongoid.org/docs/querying/

Tags: , ,