Hướng dẫn mongodb update set - bộ cập nhật mongodb

This entry is part 12 of 24 in the series MongoDB

  • MongoDB là gì? Các khái niệm trong MongoDB
  • Hướng dẫn cài đặt, cấu hình MongoDB trên Windows 10
  • Cài đặt và sử dụng Robo 3T (RoboMongo) trên Windows
  • Hướng dẫn cài đặt MongoDB trên Linux (Ubuntu 16.04)
  • Hướng dẫn MongoDB – Tạo database trong MongoDB
  • Hướng dẫn MongoDB – Xóa, DROP database trong MongoDB
  • Tạo bảng, collections trong MongoDB (Tạo bằng dòng lệnh, Robo3t)
  • Xóa bảng, collections trong MongoDB (Xóa bằng lệnh/ Robo3T)
  • Insert document, bản ghi trong MongoDB (Insert bằng lệnh, Robo3T)
  • Truy vấn dữ liệu/document, find(), Select, Where trong MongoDB
  • Xóa document, row trong MongoDB (bằng dòng lệnh, Robo3T)
  • Update document, dữ liệu trong MongoDB
  • Projection trong MongoDB (SELECT field/column trong MongoDB)
  • Sắp xếp trong MongoDB(orderBy, sorting() trong MongoDB)
  • Ưu nhược điểm của MongoDB, khi nào nên dùng MongoDB
  • Xóa cột, field, trường của collections trong MongoDB ($unset)
  • Kiểm tra null, check tồn tại trong MongoDB với $exists
  • Đổi tên field trong MongoDB với $rename
  • Tạo user/roles, phân quyền người dùng trên MongoDB
  • Các loại roles, vai trò, quyền trong MongoDB.
  • Đăng nhập mongodb với username và password (database mongo)
  • Tạo Replica Set trong MongoDB, Ví dụ Replica Set MongoDB
  • Replication, Replica Set trong MongoDB là gì?
  • Phân trang trong MongoDB (skip(), limit() paging trong MongoDB)

Update document, dữ liệu trong MongoDB.

Cú pháp:

Để update document trong MongoDB ta dùng method

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
2:

db.collection_name.update(
   ,
   ,
   {
     upsert: ,
     multi: ,
     writeConcern: ,
     collation: ,
     arrayFilters: [ , ... ]
   }
)

Trong đó:

  • {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    3: là mệnh đề where trong MongoDB dùng để chọn ra những document được update
  • {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    4: trường được update và giá trị mới được update.
  • {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    5: (boolean): mặc định là false. Nếu là true thì sẽ tạo document mới nếu không tìm thấy document nào thỏa mãn 
    {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    3
  • multi: (boolean): mặc định là false. Nếu là true thì mới cho phép update nhiều document cùng thỏa mãn 
    {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    3

Ví dụ trong collection

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
8 mình có 5 bản ghi như sau:

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},

Hướng dẫn mongodb update set - bộ cập nhật mongodb

Bây giờ muốn đổi country của player có name = ‘neymar’ thành ‘spain’ thì câu lệnh sẽ là:

db.player.update({'name':'neymar'},{$set: {'country':'spain'}})

Hướng dẫn mongodb update set - bộ cập nhật mongodb

Một số ví dụ khác:

Đổi country = ‘vn’ với các player có name = ‘ronaldo’ hoặc name = ‘modric’

db.player.update({'name': {$in :['ronaldo', 'modric']}},{$set: {'country':'vn'}}, {'multi':true})

Đổi country = ‘japan’, name = ‘honda’ với document có _id = ‘1’

db.player.update({'_id':'1'},{$set: {'country':'japan','name':'honda'}})

*Lưu ý, nếu trong phần update bạn không dùng

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
9 thì các field không được chỉ rõ sẽ bị null:, nếu trong phần update bạn không dùng
{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
9 thì các field không được chỉ rõ sẽ bị null:

Ví dụ:

db.player.update({'_id':'1'}, {'country':'japan','name':'honda'})

Lệnh trên sẽ đổi country = ‘japan’, name = ‘honda’, age = null với document có _id = ‘1’

________________________

Okay, Done!

References: https://docs.mongodb.com/manual/mongo/

Mục nhập này là phần 12/24 trong loạt MongoDB

  • MongoDB Là Gì? Các Khái niệm Trong Mongodb
  • Hướng dẫn cài đặt, cấu hÌnh MongoDb trên Windows 10
  • CÀi ĐặT VÀ Sử DụNG ROBO 3T (ROBOMONGO)
  • Hướng dẫn cài đặt MongoDB trên linux (Ubuntu 16.04)
  • Hướng dẫn MongoDB - cơ sở dữ liệu Tạo Trong MongoDB
  • Hướng dẫn MongoDB - XÓA, thả cơ sở dữ liệu Trong MongoDB
  • Tạo bảng, bộ sưu tập Trong MongoDB (tạo bằng dầmg lệNH, robo3t)
  • Xóa bảng, collections trong MongoDB (Xóa bằng lệnh/ Robo3T)
  • Insert document, bản ghi trong MongoDB (Insert bằng lệnh, Robo3T)
  • Truy vấn dữ liệu/document, find(), Select, Where trong MongoDB
  • Xóa document, row trong MongoDB (bằng dòng lệnh, Robo3T)
  • Update document, dữ liệu trong MongoDB
  • Projection trong MongoDB (SELECT field/column trong MongoDB)
  • Sắp xếp trong MongoDB(orderBy, sorting() trong MongoDB)
  • Ưu nhược điểm của MongoDB, khi nào nên dùng MongoDB
  • Xóa cột, field, trường của collections trong MongoDB ($unset)
  • Kiểm tra null, check tồn tại trong MongoDB với $exists
  • Đổi tên field trong MongoDB với $rename
  • Tạo user/roles, phân quyền người dùng trên MongoDB
  • Các loại roles, vai trò, quyền trong MongoDB.
  • Đăng nhập mongodb với username và password (database mongo)
  • Tạo Replica Set trong MongoDB, Ví dụ Replica Set MongoDB
  • Replication, Replica Set trong MongoDB là gì?
  • Phân trang trong MongoDB (skip(), limit() paging trong MongoDB)

Update document, dữ liệu trong MongoDB.

Cú pháp:

Để update document trong MongoDB ta dùng method

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
2:

db.collection_name.update(
   ,
   ,
   {
     upsert: ,
     multi: ,
     writeConcern: ,
     collation: ,
     arrayFilters: [ , ... ]
   }
)

Trong đó:

  • {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    3: là mệnh đề where trong MongoDB dùng để chọn ra những document được update
  • {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    4: trường được update và giá trị mới được update.
  • {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    5: (boolean): mặc định là false. Nếu là true thì sẽ tạo document mới nếu không tìm thấy document nào thỏa mãn 
    {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    3
  • multi: (boolean): mặc định là false. Nếu là true thì mới cho phép update nhiều document cùng thỏa mãn 
    {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
    3

Ví dụ trong collection

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
8 mình có 5 bản ghi như sau:

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},

Bây giờ muốn đổi country của player có name = ‘neymar’ thành ‘spain’ thì câu lệnh sẽ là:

db.player.update({'name':'neymar'},{$set: {'country':'spain'}})

Một số ví dụ khác:

Đổi country = ‘vn’ với các player có name = ‘ronaldo’ hoặc name = ‘modric’

db.player.update({'name': {$in :['ronaldo', 'modric']}},{$set: {'country':'vn'}}, {'multi':true})

Đổi country = ‘japan’, name = ‘honda’ với document có _id = ‘1’

db.player.update({'_id':'1'},{$set: {'country':'japan','name':'honda'}})

*Lưu ý, nếu trong phần update bạn không dùng

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
9 thì các field không được chỉ rõ sẽ bị null:, nếu trong phần update bạn không dùng
{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':'25'},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':'25'},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':'18'},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':'30'},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':'33'},
9 thì các field không được chỉ rõ sẽ bị null:

Ví dụ:

db.player.update({'_id':'1'}, {'country':'japan','name':'honda'})

Lệnh trên sẽ đổi country = ‘japan’, name = ‘honda’, age = null với document có _id = ‘1’

________________________

Okay, Done!

References: https://docs.mongodb.com/manual/mongo/

Docs Home → MongoDB ManualMongoDB Manual


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.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

db.player.update({'name':'neymar'},{$set: {'country':'spain'}})
8 field nor can you replace an existing document with a replacement document that has a different
db.player.update({'name':'neymar'},{$set: {'country':'spain'}})
8 field value.

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

  • The

    db.player.update({'name':'neymar'},{$set: {'country':'spain'}})
    8 field is always the first field in the document.

  • Updates that include

    db.player.update({'name': {$in :['ronaldo', 'modric']}},{$set: {'country':'vn'}}, {'multi':true})
    1 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.