Hướng dẫn mongodb find and update

Docs HomeMongoDB Manual


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


Note

Starting in MongoDB 4.2, MongoDB can accept an aggregation pipeline to specify the modifications to make instead of an update document. See the method reference page for details.

All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.

Once set, you cannot update the value of the _id field nor can you replace an existing document with a replacement document that has a different _id field value.

For write operations, MongoDB preserves the order of the document fields except for the following cases:

  • The _id field is always the first field in the document.

  • Updates that include renaming of field names may result in the reordering of fields in the document.

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

Tiếp tục với series phần này chúng ta sẽ tìm hiểu về các update một hoặc nhiều bản ghi trong MongoDB.

1, Sửa đổi một bản ghi trong MongoDB.

-Để sửa đổi một bản ghi duy nhất trong MongoDB thì các bạn sử dụng phương thức updateOne() theo cú pháp sau:

db.collectionName.updateOne(
    filter,
    update,
    {
        upsert: ,
        writeConcern: 
        collation: ,
    }
)

Trong đó:

  • filter là một object chứa các tiêu chí lựa chọn bản ghi update (sử dụng cú pháp selector).
  • update là object chứa dữ liệu sửa đổi trên bản ghi.
  • upsert là một boolean cấu hình điều gì sẽ xảy ra khi không có bản khi khớp với filter. Nếu upsert = true thì nó sẽ thêm mới bản ghi đó nếu không có bản ghi nào khớp với filter và sẽ không có điều gì xảy ra nếu upsert = false. Mặc định thì upsert = false.
  • writeConcern là một document chứa write concern.
  • collation là một document chứa các quy tắc.

Lưu ý: Khi sử dụng phương thức updateOne() nếu như dữ liệu khớp với filter nhiều hơn một bản ghi thì nó sẽ chỉ sửa đổi cho một bản ghi đầu tiên.

VD1: Sửa name của admin có tuổi = 18 thành Toidicode.

db.admin.updateOne(
    {age: 18},
    {
        $set: {
            name: "Toidicode"
        }
    }
)

Hướng dẫn mongodb find and update

Hình minh họa

2,  Sửa đổi nhiều bản ghi trong MongoDB.

-Để sửa nhiều bản ghi trên một lần khai báo trong MongoDB thì mọi người sử dụng phương thức updateMany() với cú pháp tương tự như Phương thức updateOne():

db.collectionName.updateOne(
    filter,
    update,
    {
        upsert: ,
        writeConcern: 
        collation: ,
    }
)

Trong đó:

  • filter là một object chứa các tiêu chí lựa chọn bản ghi update (sử dụng cú pháp selector).
  • update là object chứa dữ liệu sửa đổi trên bản ghi.
  • upsert là một boolean cấu hình điều gì sẽ xảy ra khi không có bản khi khớp với filter. Nếu upsert = true thì nó sẽ thêm mới bản ghi đó nếu không có bản ghi nào khớp với filter và sẽ không có điều gì xảy ra nếu upsert = false. Mặc định thì upsert = false.
  • writeConcern là một document chứa write concern.
  • collation là một document chứa các quy tắc.

-Lưu ý: Phương thức này chỉ khác với phương thức updateOne() ở chỗ: Nếu như số lượng bản ghi so khớp với filter lớn hơn 1 bản ghi thì nó sẽ sửa dữ liệu trên tất cả các bản ghi đó.

VD2: Sửa name của admin có name = "Vu Thanh Tai" thành Toidicode.

db.admin.updateMany(
    {name: "Vu Thanh Tai"},
    {
        $set: {
            name: "Toidicode"
        }
    }
)

Hướng dẫn mongodb find and update

Hình minh họa

3, Sửa đổi bản ghi trong MongoDB.

-Ngoài 2 phương thức trên thì trong MongoDB còn cung cấp cho chúng ta một phương thức update() có thể cấu hình updateOne() hoặc updateMany(), sử dụng theo cú pháp sau:

db.collection.update(
   filter,
   update,
   {
     upsert: ,
     multi: ,
     writeConcern: ,
     collation: 
   }
)

Trong đó:

  • filter là một object chứa các tiêu chí lựa chọn bản ghi update (sử dụng cú pháp selector).
  • update là object chứa dữ liệu sửa đổi trên bản ghi.
  • upsert là một boolean cấu hình điều gì sẽ xảy ra khi không có bản khi khớp với filter. Nếu upsert = true thì nó sẽ thêm mới bản ghi đó nếu không có bản ghi nào khớp với filter và sẽ không có điều gì xảy ra nếu upsert = false. Mặc định thì upsert = false.
  • multi là một boolean cấu hình xem có cho phép sửa đổi nhiều bản ghi hay không, multi bằng true là cho phép và ngược lại bằng false thì là không. Mặc định thì thuộc tính này có giá trị là false.
  • writeConcern là một document chứa write concern.
  • collation là một document chứa các quy tắc.

VD3: Sửa đổi name của một bản ghi duy nhất có name là "Toidicode" thành "Vu Thanh Tai".

db.admin.updateOne(
    {name: "Vu Thanh Tai"},
    {
        $set: {
            name: "Toidicode"
        }
    },
    {
        multi : false
    }
)

Hướng dẫn mongodb find and update

Hình minh họa

4, Lời kết.

-Như vậy phần này mình đã hướng dẫn đến mọi người 3 cách để sửa đổi dữ liệu trong collection rồi, bài tiếp theo chúng ta sẽ tìm hiểu về các cách xóa dữ liệu trong collection.

Đăng ký nhận tin.

Chúng tôi chỉ gửi tối đa 2 lần trên 1 tháng. Tuyên bố không spam mail!

Bài Viết Mới