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

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

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

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