Mongoid not in query
Try this query:
user = User.not_in(:_id => [2]).second
In MongoDB primary key has name _id
. Mongoid tries to be friendly and partially hides this fact from the developer by aliasing it to id
in the object model. But when you do queries, it cannot tell if you want primary key _id
or some completely ordinary field id
.
How make a query in to Mongoid, 'where is not null'?
You can write using $ne
where(:comment_id.ne => nil)
Relevant documentation of queries.
Select where not null or empty in mongoid
Try
Model.where(:url.nin => ["", nil]).count
It works even when url = nil
Mongoid 6.3 where in query not working
Rails 5 and Mongoid 6.3:
User.in(role: ['admin', 'supervisor']).count # recommended
OR
User.any_of(:role.in => ['admin', 'supervisor']).count
Rails 4 and Mongoid 5.1:
User.where(:role.in => ['admin', 'supervisor']).count
OR
User.in(role: ['admin', 'supervisor']).count
How to do mongoid 'not_in' 'greater than' query
model without attribute greater than 100 = model with attribute less than or equal to 100?
Model.where({'price' => {'$lte' => 100}})
How do I do a NOT IN query in Mongo?
You can use $in or $nin for "not in"
Example ...
> db.people.find({ crowd : { $nin: ["cool"] }});
I put a bunch more examples here: http://learnmongo.com/posts/being-part-of-the-in-crowd/
Mongoid query with AND and OR condition
It should work
StateTransaction.where(current: true, :name.ne => ["",nil]).and(
StateTransaction.or(
{:position.in =>["SPVR", "CASH"]},
{sector: "BREAK"}
).selector
)
Related Topics
Limiting Characters/Words in View - Ruby on Rails
Require Lib in Rspec with Ruby 1.9.2 Brings "No Such File to Load"
How to Fix Libv8 Error from Gemfile on Mavericks
How to Embed Ruby in JavaScript (Rails + .Html.Erb File)
Are There Any Good Mutation Testing Tools for Ruby 1.9 and Rspec2
How to Find a Memory Leak on Heroku
Setting Environment Variables with Puppet
Dealing with Large CSV Files (20G) in Ruby
Ruby W/ Sinatra: What Is the Equivalent of a .Js.Erb from Rails
Carrierwave How to Get the File Extension
Actiondispatch::Http::Uploadedfile.Content_Type Not Being Initialized in Rspec Test
Enumerator as an Infinite Generator in Ruby
Ruby on Rails: Conditionally Display a Partial
Rails 12 Hour Am/Pm Range for a Day