1

User Schema

const UserSchema = new mongoose.Schema({
name : {
    type: String,
    required : true
},
email : {
    type: String,
    required : true
},
password : {
    type: String,
    required : true
},
date : {
    type: Date,
    default : Date.now,
},
todo : [{ type : mongoose.Schema.Types.Mixed,ref : 'Todo'}]
})

 const User = mongoose.model('User',UserSchema);

 module.exports = User;

Todo Schema

const TodoSchema = ({
task : String
})

const Todo = mongoose.model('Todo', TodoSchema)

module.exports = Todo;

Database enter image description here

How do I delete a single todo object i.e("Task 1") from the user?

router.get('/delete/:id',ensureAuthenticated, (req,res)=>{
id = req.params.id
user = req.user
User.update({ }, { "$pull": { task: id }});
tasks = user.todo
res.render("todo/all",{
todo:tasks,  
});
})

I have tried all the stackoverflow threads for over 4 hours and I still coudn't figure out what's wrong. Really apprecitate it if you could help it out. Thank You :)

Chaitanya
  • 41
  • 6
  • Does this answer your question? [MongoDB, remove object from array](https://stackoverflow.com/questions/15641492/mongodb-remove-object-from-array) – turivishal Jun 18 '21 at 17:13
  • `User.findOneAndUpdate({ _id:user.id },{ $pull:{ todo:{_id : id} } })` I have tried using that tread solution like this but it doesn't seem to work? – Chaitanya Jun 18 '21 at 17:38
  • your query looks good, it should work. make sure you have used `await` before query and wrapped function by `async` – turivishal Jun 18 '21 at 17:43

0 Answers0