eolas/neuron/10a99c7d-3445-4f21-a0e2-3c028de3bb75/Deleting_Mongo_documents.md
2024-10-23 15:05:28 +01: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.