Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda