eolas/zk/Deleting_documents.md

21 lines
503 B
Markdown
Raw Normal View History

2022-08-13 12:30:03 +01:00
---
2022-08-16 11:58:34 +01:00
categories:
- Databases
2022-08-20 15:00:05 +01:00
tags: [mongo-db, node-js, mongoose]
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`.