How to Do Mongoid 'Not_In' 'Greater Than' Query

If I want to search a mongoid model with attribute greater than 100 I would do this.

Model.where({'price' => {'$gt' => 100}})

How do I do search a mongoid model without attribute greater than 100?

Tried this and failed.

Model.not_in({'price' => [{'$gt' => 100}]})

Additional info:

In the end of the day would like to make a query like so:

criteria = {
    'price' => [{'$gt' => 100}],
    'size'  => 'large',
    'brand' => 'xyz'    
}

Model.not_in(criteria)

As the criteria would be dynamically created.

3 Answers

model without attribute greater than 100 = model with attribute less than or equal to 100?

Model.where({'price' => {'$lte' => 100}})
1

Try this

 Model.where(:price.lte => 100,:size.ne => 'large',:brand.ne => 'xzy')
1

Try using the .ne() (not equals) operator

Model.where({:price.lte => 100}).ne({:size => 'large', :brand => 'xzy'})

You can also find the Mongoid documentation here

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest