Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

How to do mongoid 'not_in' 'greater than' query

Writer Sebastian Wright

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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