4

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.

1 Answer 1

2

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 
    }
  } 
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.