Mongoid 6.3 where in query not working

103 Views Asked by At

Rails app with MongoDB.

Rails version 5.1.6 And mongoid version 6.3.0.

Following query doesn't seem to work:

User.where(:role.in => ['admin', 'supervisor']).count

Above query returns 0. But following query

User.where(:role.in => 'supervisor')

returns appropriate result.

Am I doing it wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

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