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

120
Visualizações
Add a new field in mongoDB from another collection independently?

I have two collections: profiles and contents

The profiles collection looks like this:

{
  _id: ObjectId('618ef65e5295ba3132c11111'),
  blacklist: [ObjectId('618ef65e5295ba3132c33333'), ObjectId('618ef65e5295ba3132c22222')],
  //more fields
}

The contents collection looks like this:

{
  _id: ObjectId('618ef65e5295ba3132c00000'),
  owner: ObjectId('618ef65e5295ba3132c22222'),
  //more fields
}

What I need is to get those contents where owner is not included in the blacklist. I thought about to put the blacklist field into the contents documents. I could get the profile by id separately (in another query) and set it manually in the aggregation where I get the contents, but this requires one extra connection.

So my question is: Is there a way to add my profile into each document of another collection? keep in mind that I have the profile ID.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is some psuedo code on how it "should" look like, First fetching the blacklists owners, then using that variable as a parameter in the pipeline.

const profileId = input;
const blackListIds = await db.getCollection('profiles').distinct('blacklist', { _id: profileId });
const aggregationResults = await db.getCollection('contents').aggregate([
    {
        $match: {
            owner: {$nin: blackListIds}
        }
    }
    ... continuation of pipeline ...
])
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