This entry is part 12 of 24 in the series 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: //docs.mongodb.com/manual/mongo/
Mục nhập này là phần 12/24 trong loạt 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: //docs.mongodb.com/manual/mongo/
Docs Home → MongoDB Manual → MongoDB 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.