Question:
What is Nodejs method for Mongoose - Accessing and removing a subdocument?

To Access and remove a subdocument by _ID within an array of objects in the Nodejs method for Mongoose, you need the> $pull operator to remove the matched object from the items array.


await <Party model>.updateOne({

  "items._id": _id

},

{

  $pull: {

    "items": {

      _id: _id

    }

  }

})


>Demo @ Mongo Playground


Answered by:> Yong Shun

Credit:> StackOverflow


Blog Links: 

>Why Swagger UI does not show the Choose a File button?

>How to measure the time queries take to run in NodeJS?

>While npm run dev do nothing and crash in Nodejs?

>How to update a document in MongoDB with NodeJS?

>How can I write a new file in Nodejs?

>How to overwrite the contents of MongoDB via a put request in Nodejs?

>NodeJS Error Solved: Handling Not Sending Error To Frontend

>How to wait until I have my uploaded URL?


Nisha Patel

Nisha Patel

Submit
0 Answers