Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
Mongoid query with AND and OR condition

I am using a MongoDB for a new app I am creating and I only need certain data from it. I require the following fields: current (boolean), position, sector, and name. The position field is populated with one of four values - "cash, SPVR, OFF, and training", while the sector field has one of two values - "Break or Working". I want a query that will give me all records where the following conditions exist: 1. current = TRUE 2. Position = "SPVR" or "CASH" OR Sector = "BREAK" and 3. Name is not NULL

The problem I have is that there are hundreds of records that have a value of BREAK, but most of them do not have a value in the Name field. I don't want any records that do not have a name associated with them. So in that respect, in line 2 above, I'm guessing the OR could be AND?

This is mu current start at the Mongoid statement

get '/currentstate*' do
    StateTransaction.where( current: true, :position.in =>["SPVR", "CASH"] ).to_json

  end

Any help would be greatly appreciated.

Thanks!!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It should work

StateTransaction.where(current: true, :name.ne => ["",nil]).and(
  StateTransaction.or(
    {:position.in =>["SPVR", "CASH"]},
    {sector: "BREAK"}
  ).selector
)
over 4 years ago · Santiago Trujillo Denunciar

0

Following is arguably simpler:

sts = StateTransaction.where(current: true, :name.ne => ["",nil])
sts.any_of(:position.in => ["SPVR", "CASH"], sector: 'BREAK')
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda