Hướng dẫn mongodb update set - bộ cập nhật mongodb
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( Trong đó:
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: 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 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( Trong đó:
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: 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 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. NoteStarting 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:
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern. |