Sử dụng toán tử $pull trong update() để xóa tất cả phần tử khớp giá trị trong mảng.
Tạo collection demo125:
> db.demo125.insertOne({"ListOfNames":["John","Chris","Bob","David","Carol"]});
Xóa "David" khỏi mảng ListOfNames (multi:true để áp dụng nhiều document):
> db.demo125.update(
{ },
{ $pull: { ListOfNames: { $in: ["David"] } } },
{ multi: true }
)
Kết quả - David đã bị xóa:
{
"_id" : ObjectId("5e2f304068e7f832db1a7f55"),
"ListOfNames" : [ "John", "Chris", "Bob", "Carol" ]
}
Lưu ý: Từ MongoDB 3.2+, nên dùng updateMany() thay vì update({}, ..., {multi:true}).