eolas/neuron/0c4abb9b-c940-4636-84e0-b880b7c1ac8b/Deleting_Mongo_documents.md
2025-01-02 18:02:35 +00:00

499 B

tags
mongo-db
node-js
mongoose
databases

MongoDB: deleting a document from a collection

async function deleteCourse(id) {
  const result = await Course.deleteOne({ id: id });
  console.log(result);
}

Use findByIdAndRemove([id]) if you want to return the value that is deleted.

If we were to pass a query object that matches multiple documents in the database to deleteOne it would only delete the first document returned. To delete all use deleteMany.