eolas/neuron/3962ef80-32e3-49b8-8e16-d14a26e9787a/Deleting_Mongo_documents.md
2024-10-22 16:47:58 +01:00

22 lines
499 B
Markdown

---
tags:
- mongo-db
- node-js
- mongoose
- databases
---
# MongoDB: deleting a document from a collection
```js
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`.