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

130
Vistas
find from last element's(object) field in array of objects in mongoose

How to find data conditionally with mongoose by checking state field last element of history array.

{
        "_id" : ObjectId("5ff4360b3a1119002a6b8e04"),
        "name" : "sample"
        "history" : [ 
            {
                "_id" : ObjectId("56b0402a92b2a18116bf51de"),
                "state" : "pending"
            }, 
            {
                "_id" : ObjectId("56b0402a92b2a18116bf51fn"),
                "state" : "completed"
            }
        ],

    }

I try with following,

collection.find({$expr: {$eq: [{"$arrayElemAt": ["stateHistory", -1]}, "completed"]}})

Is there way to do this using find function without using aggregation?

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

0

you can use $where

collection.find({
    $where: function(){
        return this.history[this.history.length-1].state === "completed"
    }
})
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