Nâng CaoMongoDB iconMongoDB

Upsert trong MongoDB là gì?

Upsert ("update + insert") là một tùy chọn của lệnh update, vd updateOne(filter, update, { upsert: true }).

Cơ chế: nếu có document khớp filterupdate nó; nếu không có → insert một document mới ghép từ giá trị trong filter và các toán tử update. Rất tiện cho đồng bộ dữ liệu hoặc tăng/đếm counter ("nếu chưa có thì tạo, có rồi thì cộng dồn").

Xem toàn bộ MongoDB cùng filter theo level & chủ đề con.

Mở danh sách MongoDB