0

I have a user schema with an array of task ObjectId's and I'm trying to figure out how I can go about removing them.

User Schema

const UserSchema = new Schema({
        email: {
            type: String,
            lowercase: true,
            index: { unique: true }
        },
        password: {
            type: String,
        },
    tasks: [{
        type: Schema.Types.ObjectId,
        ref: 'task'
    }]
})

Below is the code I've tried to use but I'm not sure where to go from with what I've done so far.

router.delete('/deleteUsersTasks/:_id', async(req, res, next) => {
    User.findById({_id: req.params._id}, (err, user) => {
        User.findByIdAndRemove(tasks: req.body._id)
});

Is there a better way to remove the task ObjectId from the tasks array in the user schema or am I just missing something in my route currently?

Sharkows Abel
  • 13
  • 1
  • 6
  • Possible duplicate of [How to remove array element in mongodb?](https://stackoverflow.com/questions/16959099/how-to-remove-array-element-in-mongodb) – Ayush Gupta Oct 30 '18 at 06:33
  • Found a solution that worked for me [here](https://stackoverflow.com/questions/35973960/mongoose-remove-object-from-array-based-on-id-cast-error) – Sharkows Abel Oct 30 '18 at 07:06

0 Answers0