Home > Back-end >  Unable to delete element from array of objects using mongoose
Unable to delete element from array of objects using mongoose

Time:09-17

I am trying to remove one element from array of objects in MongoDB.

Please find the schema structure below.

enter image description here

I just want to remove one object by status, ideaID and invitedBy. Please find the query I am using for it,

    await User.findByIdAndUpdate(
    currentUser,
    { $pull: { "invitationStatus.ideaId": this.req.body.ideaId, "invitationStatus.status": "pending", "invitationStatus.invitedBy": getUserByNotificationId.createdBy._id } })

but this query is not removing the specified object.

CodePudding user response:

You have to specify from which field you want to pull item. Change your query like this:

await User.findByIdAndUpdate(currentUser, { 
  $pull: { 
    invitationStatus: {
      ideaId: this.req.body.ideaId,
      status: "pending", 
      invitedBy: getUserByNotificationId.createdBy._id 
    }
  } 
})
  • Related