Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Cách hủy đặt (unset) biến trong MongoDB shell

Sử dụng toán tử delete trong JavaScript shell. Cú pháp:

delete yourVariableName;

Ví dụ minh họa

Biến chưa định nghĩa:

> customerDetail;

Lỗi: ReferenceError: customerDetail is not defined

Gán giá trị:

> customerDetail = {"CustomerFirstName": "Chris"};

Kết quả: { "CustomerFirstName": "Chris" }

Hiển thị biến:

> customerDetail;

Kết quả: { "CustomerFirstName": "Chris" }

Hủy biến:

> delete customerDetail;

Kết quả: true

Kiểm tra lại:

> customerDetail;

Lỗi: ReferenceError: customerDetail is not defined

Lưu ý: delete chỉ xóa biến trong shell session, không ảnh hưởng đến dữ liệu trong database. Để xóa trường trong tài liệu, dùng $unset trong update.