eolas/zk/Deleting_Mongo_documents.md

23 lines
499 B
Markdown
Raw Permalink Normal View History

2022-08-13 12:30:03 +01:00
---
2024-06-15 11:15:03 +01:00
tags:
- mongo-db
- node-js
- mongoose
- databases
2022-08-13 12:30:03 +01:00
---
# MongoDB: deleting a document from a collection
```js
async function deleteCourse(id) {
const result = await Course.deleteOne({ id: id });
2022-08-16 11:58:34 +01:00
console.log(result);
2022-08-13 12:30:03 +01:00
}
```
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`.