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

215
Visualizações
Remove items from Mongo Aggregation if contained in an item array

Using MongoDB Node.js

I have an products collection:

Example:

{
    _id: '12',
    linkedProducts: ['1234']
}

If I had a product ID with an ID of 1234, and it is linked to '12', what would be the best way to lookup through all products that has 1234 as a linked product?

Any help would be greatly appreciated.

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

0

You can first use the $in operator on the array field to find documents where the array contains the given id and then use $pull to remove this value from the array:

db.collection.update({
  linkedProducts: {
    $in: [
      "1234"
    ]
  }
},
{
  $pull: {
    linkedProducts: "1234"
  }
},
{
  multi: true
})

Here's a working example on mongoplayground: https://mongoplayground.net/p/nlBUWLdKERS

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