Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda