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) {
|
2024-02-02 15:58:13 +00:00
|
|
|
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.
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
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`.
|