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

120
Vistas
Finding parent of an element in mongodb

I'm trying to get the parent of an element in a document but I'm not quite sure how. Could I please get some help?

This is what my collection looks like

students:Array
    0:Object{
        age:21
        studentName:"ABC"
        height: "178"
      },
      1:Object{
        age:25
        studentName:"DSA"
        height: "185"
      }

I want to be able to find

    0:Object{
        age:21
        studentName:"ABC"
        height: "178"
      }

When I search for studentName = "ABC"

Could I please get some help?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

if you first fetch data from mongodb then you can do it:

const urStudent = students.filter((student)=> student.studentName === "ABC")

but if you want just to fetch a certain student, so you should change the query. something like that:

 db.Students.findOne({studentName:"ABC"}).then((res)=>{...})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Query1
(using find)

Test code here

db.collection.find({
  students: {
    $elemMatch: {
      studentName: "ABC"
    }
  }
},
{
  "students.$": 1
})

Query2
(using aggregation)

Test code here

aggregate(
[{"$project": 
   {"_id": 0,
    "student": 
    {"$arrayElemAt": 
     [{"$filter": 
        {"input": "$students",
          "cond": {"$eq": ["$$this.studentName", "ABC"]}}},
      0]}}}])
about 4 years ago · Juan Pablo Isaza 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