> For the complete documentation index, see [llms.txt](https://janeto.gitbook.io/mongodb-toan-tap/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://janeto.gitbook.io/mongodb-toan-tap/cap-nhat-document-trong-mongodb/phuong-thuc-update-trong-mongodb.md).

# Phương thức update() trong MongoDB

Phương thức update() cập nhật các giá trị trong Document đang tồn tại.

## Cú pháp <a href="#c-ph-p" id="c-ph-p"></a>

Cú pháp cơ bản của phương thức update() là như sau:

```
>db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
```

## Ví dụ <a href="#v-d" id="v-d"></a>

Bạn theo dõi Collection có tên mycol có dữ liệu sau:

{ "\_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}

{ "\_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}

{ "\_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}

Ví dụ sau sẽ thiết lập tiêu đề mới 'New MongoDB Tutorial' của Document có tiêu đề là 'MongoDB Overview':

```
>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})

>db.mycol.find()

{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}

{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}

{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}

>
```

Theo mặc định, MongoDB sẽ chỉ cập nhật một Document đơn, để cập nhật nhiều Document, bạn thiết lập tham số 'multi' thành true.

```
>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
```
