eolas/Databases/MongoDB/Update_document.md

74 lines
2.4 KiB
Markdown
Raw Normal View History

2022-08-13 10: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 10:30:03 +01:00
---
# Update a MongoDB document
There are two methods for updating a document
2022-08-16 11:58:34 +01:00
- query first
- update first
2022-08-13 10:30:03 +01:00
## Query first document update
2022-08-16 11:58:34 +01:00
With this approach we first execute a [query](/Databases/MongoDB/Querying_a_collection.md) to retrieve the document we want to edit and then make the change. We use the `findById` method to identify the document by its UUID and then `set` to update specified properties on the document. The `set` method is one of many operators that can be used to update values. For example there is also built-in operators for increment, renaming, multiplying values.
2022-08-13 11:00:04 +01:00
Query first is best used when you want to secure the update with some prior logic or to validate. For example you may not want to update a course if it is listed as published. You could use a query to determine the publish status and then only update the entry if it returns `isPublished: false`.
2022-08-13 10:30:03 +01:00
```js
async function updateCourseFromQuery(id) {
2022-08-16 11:58:34 +01:00
const course = await Course.findById(id);
if (!course) return;
course.set({
isPublished: true,
author: "A new author",
2022-08-16 11:58:34 +01:00
});
// Instead of an object, we could also set the updated properties individually
course.isPublished = true;
course.author = "Biggs Volunksire";
2022-08-16 11:58:34 +01:00
const result = course.save();
console.log(result);
2022-08-13 10:30:03 +01:00
}
2022-08-13 11:00:04 +01:00
```
## Update first document update
2022-08-16 11:58:34 +01:00
With this approach we don't bother with a prior query. We are confident that the update is legitimate and do not need to first determine that certain conditions are met.
2022-08-13 11:00:04 +01:00
To do this we directly use the `update` method, not `find`:
2022-08-13 10:30:03 +01:00
2022-08-13 11:00:04 +01:00
```js
async function updateCourseFromQuery(id) {
const result = await Course.update({ _id: id });
2022-08-16 11:58:34 +01:00
$set: {
// Invoke the set operator
author: "Terry Nutile";
2022-08-16 11:58:34 +01:00
isPublished: true;
}
console.log(result);
2022-08-13 11:00:04 +01:00
}
2022-08-13 10:30:03 +01:00
```
2022-08-13 11:00:04 +01:00
This function will just return some metadata about the update. It won't by default return the updated value. To do this use the `findByIdAndUpdate()` method instead of `update`:
```js
async function updateCourseFromQuery(id) {
const course = await Course.findByIdAndUpdate(id, {
2022-08-16 11:58:34 +01:00
$set: { // Invoke the set operator
2022-08-13 11:00:04 +01:00
author: 'Terry Nutile'
2022-08-16 11:58:34 +01:00
isPublished: true
2022-08-13 11:00:04 +01:00
}, {new: true}});
console.log(result)
}
```
2022-08-16 11:58:34 +01:00
If we don't add `{new: true}`, it will return the document before the update.
2022-08-13 11:00:04 +01:00
### Updating multiple documents at once
// Add: notes on this