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

563
Vistas
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 Respuestas
Responde la pregunta

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 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