How to delete documents in mongo with mongoose

To delete one entry you can use findOneAndRemove command – it issues a mongodb findAndModify remove command.
Finds a matching document, removes it, passing the found document (if any) to the callback.

let deleteBookmarkById = async (userId, bookmark…


This content originally appeared on DEV Community and was authored by Adrian Matei

To delete one entry you can use findOneAndRemove command - it issues a mongodb findAndModify remove command.
Finds a matching document, removes it, passing the found document (if any) to the callback.

let deleteBookmarkById = async (userId, bookmarkId) => {
  const bookmark = await Bookmark.findOneAndRemove({
    _id: bookmarkId,
    userId: userId
  });

  if ( !bookmark ) {
    throw new NotFoundError('Bookmark NOT_FOUND with id: ' + bookmarkId);
  } else {
    return true;
  }
};

An alternative is to use the deleteOne() method which deletes the first document that matches conditions from the collection. It returns an object with the property deletedCount indicating how many documents were deleted:

let deleteBookmarkById = async (userId, bookmarkId) => {
  const response = await Bookmark.deleteOne({
    _id: bookmarkId,
    userId: userId
  });

  if ( response.deletedCount !== 1 ) {
    throw new NotFoundError('Bookmark NOT_FOUND with id: ' + bookmarkId);
  } else {
    return true;
  }
};

To delete multiple documents use the deleteMany function. This deletes all the documents that match the conditions specified in filter. It returns an object with the property deletedCount containing the number of documents deleted.

/**
 * Delete bookmarks of a user, identified by userId
 */
let deleteBookmarksByUserId = async (userId) => {
  await Bookmark.deleteMany({userId: userId});
  return true;
};


Reference -

https://mongoosejs.com/docs/api/model.html

Shared with ❤️ from Codever. Use 👉 copy to mine functionality to add it to your personal snippets collection.


This content originally appeared on DEV Community and was authored by Adrian Matei


Print Share Comment Cite Upload Translate Updates
APA

Adrian Matei | Sciencx (2022-02-17T07:19:00+00:00) How to delete documents in mongo with mongoose. Retrieved from https://www.scien.cx/2022/02/17/how-to-delete-documents-in-mongo-with-mongoose/

MLA
" » How to delete documents in mongo with mongoose." Adrian Matei | Sciencx - Thursday February 17, 2022, https://www.scien.cx/2022/02/17/how-to-delete-documents-in-mongo-with-mongoose/
HARVARD
Adrian Matei | Sciencx Thursday February 17, 2022 » How to delete documents in mongo with mongoose., viewed ,<https://www.scien.cx/2022/02/17/how-to-delete-documents-in-mongo-with-mongoose/>
VANCOUVER
Adrian Matei | Sciencx - » How to delete documents in mongo with mongoose. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/17/how-to-delete-documents-in-mongo-with-mongoose/
CHICAGO
" » How to delete documents in mongo with mongoose." Adrian Matei | Sciencx - Accessed . https://www.scien.cx/2022/02/17/how-to-delete-documents-in-mongo-with-mongoose/
IEEE
" » How to delete documents in mongo with mongoose." Adrian Matei | Sciencx [Online]. Available: https://www.scien.cx/2022/02/17/how-to-delete-documents-in-mongo-with-mongoose/. [Accessed: ]
rf:citation
» How to delete documents in mongo with mongoose | Adrian Matei | Sciencx | https://www.scien.cx/2022/02/17/how-to-delete-documents-in-mongo-with-mongoose/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.