Mongoid Not in Query

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



Leave a reply



Submit