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

116
Visualizações
Show result from Two collection and apply filter on that result data

I have a situation where i need to display combined data from two collections. After displaying that result data i want to apply filter on this result data.

Can anyone suggest way or can show example of this flow? Here is my data with out put

User

{ 
    "_id" : "a6s54fas46456",
    "studentId" : "qYt5yHiZfbGrvXWak",
    "name" : "name1"
}
{ 
    "_id" : "asdf46asd4f", 
    "studentId" : "e2r4nTRiZZRsX6Xgu",
    "name" : "name2"
}
{ 
    "_id" : "asdf463443asd4f", 
    "studentId" : null,
    "name" : "name3"
}
{ 
    "_id" : "a6s54fas42346456", 
    "studentId" : "qYt5yHiZfbGrvXWak",
    "name" : "name4"
}
{ 
    "_id" : "asdf46asd6542344f", 
    "studentId" : null,
    "name" : "name3"
}

Student

{ 
    "_id" : "e2r4nTRiZZRsX6Xgu", 
    "studentName" : "student2"
}
{ 
    "_id" : "qYt5yHiZfbGrvXWak", 
    "studentName" : "student1"
}

OutPut

{
UserId: a6s54fas46456
StudentId : "qYt5yHiZfbGrvXWak"
"name" : "name1"
"studentName" : "student1"
}
{
UserId: asdf46asd4f
StudentId : "e2r4nTRiZZRsX6Xgu"
"name" : "name2"
"studentName" : "student1"
}
{
UserId: asdf463443asd4f
StudentId : null
"name" : "name3"
"studentName" : null
}
{
UserId: a6s54fas42346456
StudentId : "qYt5yHiZfbGrvXWak"
"name" : "name4"
"studentName" : "student1"
}
{
UserId: asdf46asd6542344f
StudentId : null
"name" : "name3"
"studentName" : null
}

Thanks

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

0

to get data from two collection you should use $lookup and $unwind with preserveNullAndEmptyArrays:true:true

db.users.aggregate([
    {$lookup:{from:"students", localField:"studentId", foreignField:"_id", as:"student"}},
    {$unwind:{path:"$student",preserveNullAndEmptyArrays:true}},
    {$project:{
        "UserId":"$_id",
        "StudentId" : "$student._id",
        "name" : 1,
        "studentName" : "$student.studentName"
        }
    }
])
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