I am trying to remove one element from array of objects in MongoDB.
Please find the schema structure below.
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
}
}
})