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

243
Vistas
What is the proper way to delete all elements from an array?

I am having trouble in deleting all elements from an array in mongodb. This is my schema:

const userSchema=mongoose.Schema({
    name: String,
    email: String,
    password: String,
    blog: [{
        date: Date,
        post: String
    }],
    week: [{
        weekday: String,
        tasks:[String]
    }],
    todo: [String]
});

const User= mongoose.model("User", userSchema);

I want to delete all data from todo but it is not working. Here is the code for that:

app.post("/deletelist",(req,res) =>{
    console.log(req.body);
    const user = req.body.userid;
    console.log(user);

    // User.find({_id: user}, async function(err, foundUser){
    //     if(err){
    //         console.log(err);
    //     } else{
    //         foundUser.updateOne({},{ $set : {"todo": [] }} , {multi:true});
    //         // await foundUser.save();
    //         res.send("ToDo Deleted");
    //     }
    // });

    User.updateOne({_id:user},{ $set : {"todo": []}}, {multi:true});
    res.send("ToDo Deleted");

});

I am getting user from the frontend. The part that is commented and the other one(User.updateOne) both are not working. I'll be glad if anyone can help

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

this will remove all the elements even the key if you want to remove the elements only try @Abdelrhman answer

User.updateOne({_id:user},{$unset:{todo:""}})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Updated

You cannot use multi-option in the updateOne method remove it and it will work

Example

User.updateOne({_id:user},{ $set : {"todo": []}})
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