Hướng dẫn mongodb unset
This entry is part 16 of 24 in the series MongoDB Xóa cột, field, trường của collections trong MongoDB ($unset). (Trong mongodb data lưu dưới dạng JSON nên sẽ không có khái niệm cột, tuy nhiên khi hiển thị ta sẽ thấy mỗi field tương đương với 1 cột) Để xóa một field trong collections ta sử dụng lệnh Cú pháp: Trong đó: Ví dụ: Mình insert 5 bản ghi sau vào document db.player.insert([ {'_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'}]) Để xóa fields db.player.updateMany({},{$unset:{age:""}}) hoặc: db.player.update({},{$unset:{age:""}},{multi: true}) * Lưu ý: Các bạn nhớ là dùng lệnh Kết quả: trường Okay, Done! References: https://docs.mongodb.com/manual/reference/operator/update/unset/ Docs Home → MongoDB Manual NoteDisambiguationThe following page refers to the update operator
$unset The
The specified value in the To specify a Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details. If the field does not exist, then When used with Starting in MongoDB 5.0, Create the
Update the first document in the
TipSee also: |