Querying in MongoDB using Rails
<p>I have a Product class which contains about 2 million of data. Each row in Product table has a field called property which contains hash values. I have search box and the value entered in search box should be searched within the hash values of the Products table. How do I do that? I am using MongoDB and Rails. This is what I tried so far. </p>

<pre><code>@product3= Product.select{|hash| hash.property.has_value? /.*#{params[:search]}*./i}
</code></pre>

<p>It returns undefined method 'select' for Product class. So I tried like below.</p>

<pre><code>@product3= Product.all.to_a.select{|hash| hash.property.has_value? /.*#{params[:search]}*./i}
</code></pre>

<p>But it is looping to all the data(2 million) which takes bunch of time.</p>

<p>EDIT: Image for sample data from Products table. 
I have shown 2 records from products table using the query <code>Product.limit(2).all.to_a</code>. </p>

<p><a href="https://i.stack.imgur.com/cGwOP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cGwOP.png" alt="enter image description here"></a></p>