You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/40-CRUD/5-UPDATE.mdx
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -15,11 +15,11 @@ db.collection.updateOne(
15
15
)
16
16
```
17
17
18
-
-`<query>`: Specifies which document to update.
18
+
-`<query>`: Specifies which document to update
19
19
20
20
-`<update operation>`: Defines modifications using update operators like $set, $inc, etc.
21
21
22
-
-`<options>`: (Optional) Allows additional configurations like upsert: true.
22
+
-`<options>`: (Optional) Allows additional configurations like upsert: true
23
23
24
24
## Example: Update a book
25
25
@@ -30,15 +30,15 @@ db.reviews.updateOne(
30
30
);
31
31
```
32
32
33
-
### Equivalent SQL Query
33
+
### Equivalent SQL query
34
34
35
35
```sql
36
36
UPDATE reviews SET rating =5WHERE bookId ='0312979509';
37
37
```
38
38
39
39
## Upsert option
40
40
41
-
Let's say, we want to update a review in our database from "John" for the book "0312979509" by rating it 5 stars.
41
+
Let's say we want to update a review in our database from "John" for the book "0312979509" by rating it 5 stars.
42
42
43
43
```js
44
44
db.reviews.updateOne(
@@ -47,9 +47,9 @@ db.reviews.updateOne(
47
47
);
48
48
```
49
49
50
-
Suppose "John" had never posted a review for this book before, the above query won't really do anything.
50
+
Suppose "John" had never posted a review for this book before. The above query won't really do anything.
51
51
In some cases, we may want to store a fresh new review based on the condition and updates mentioned in query.
52
-
To handle such scenarios in MongoDB, we don't have to write additional application code to achieve this, all we have to do is- utilize the `upsert` option.
52
+
To handle such scenarios in MongoDB, we don't have to write additional application code to achieve this. All we have to do is utilize the `upsert` option.
53
53
54
54
```js
55
55
db.reviews.updateOne(
@@ -61,7 +61,7 @@ db.reviews.updateOne(
61
61
62
62
:::info
63
63
64
-
Executing the above command, will insert a fresh new document in the collection like this:
64
+
Executing the above command will insert a fresh new document in the collection, like this:
0 commit comments