eolas/neuron/9445c5fd-135c-4b0b-a70c-7a6fd45d9d58/Deleting_Mongo_documents.md

23 lines
499 B
Markdown
Raw Normal View History

2024-12-09 18:34:15 +00:00
---
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`.