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

559
Visualizações
Is there a way to compare two fields in a MongoDB query

If I have a collection called game that has a schema like this.

{"team": "A", "opponent": "B", "points": 101, "opp_points": 100}
{"team": "B", "opponent": "A", "points": 100, "opp_points": 101}

Is there a way to query on a comparison between two fields in the same collection?

The SQL equivalent would be.

SELECT * FROM game where points > opp_points;

I have looked through Mongo's documentation and haven't found this, but thought they probably have it.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use $expr in $match in aggregation or in $find

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $gt: [
          "$points",
          "$opp_points"
        ]
      }
    }
  }
])

Working Mongo playground of aggregation

db.collection.find({
  $expr: {
    $gt: [
      "$points",
      "$opp_points"
    ]
  }
})

Working Mongo playground of find

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