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

139
Visualizações
MongoDB Update/delete Multiple documents based on Custom ID

I have a mongo schema

   const taskSchema=new Schema({
    userID:{type:ObjectId,required:true},
  task: {
    type: String,
    required: true,
    trim: true,
    maxlength: 30,
  },
  finalDate:{type:Date,required:true},
  isFinished:{type:Boolean,required:true}
 })

what I need to get all the documents with a particular UserID and update/delete . I can use the find method to find all the documents with the required user id . After that I am at a loss on what to do.There will be multiple documents with same user id . Any ideas on how to proceed will be a great help

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

0

Update

To update the first document matching the given condition

db.collection.update({
  "userID": ObjectId("5b87780c9ca8b03096a33380"),
  "task": "Cooking"
},
{
  $set: {
    "isFinished": true
  }
})

Test Here

To update all documents matching the given condition use multi: true

db.collection.update({
  "userID": ObjectId("5b87780c9ca8b03096a33380"),
  "task": "Cooking"
},
{
  $set: {
    "isFinished": true
  }
},
{
  multi: true
})

Test Here

Delete

To delete the first document matching the given condition

db.collection.delete({ 
  "userID": ObjectId("5b87780c9ca8b03096a33380"),
  "task": "Cooking"
})

To delete all documents matching the given condition

db.collection.deleteMany({ 
  "userID": ObjectId("5b87780c9ca8b03096a33380"),
  "task": "Cooking"
})
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