How to do mongoid 'not_in' 'greater than' query
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