MongoDB cung cấp các loại toán tử cập nhật mảng khác nhau để cập nhật giá trị của các trường mảng trong tài liệu và_______24operator là một trong số đó. Toán tử này được sử dụng để loại bỏ tất cả các trường hợp của giá trị hoặc giá trị khớp với điều kiện đã chỉ định khỏi mảng hiện có
cú pháp
{ $pull: { : , : , .. } }
Here, can specify with dot notation in embedded/nested documents or an array.
- In
> db.removeObjectFromArrayDemo.find[].pretty[];
4operator, if you specify the and the array contains the embedded/nested documents, then this operator applies the as if each array item were a document in a collection. - If you specify a in the
> db.removeObjectFromArrayDemo.find[].pretty[];
4operator to remove is an array, then this operator will remove only those items in the array that match the specified . Here, the order must be same. - If you specify a in the
> db.removeObjectFromArrayDemo.find[].pretty[];
4operator to remove is a document, then this operator will remove only those items in the array that have exact same fields and values. Here, the order of the fields can differ. - Bạn có thể sử dụng toán tử này với các phương thức như update[], findAndModify[], v.v. , theo yêu cầu của bạn
Trong các ví dụ sau, chúng tôi đang làm việc với
Cơ sở dữ liệu. GeeksforGeeks
Bộ sưu tập. cộng tác viên
Tài liệu. hai tài liệu chứa thông tin chi tiết về người đóng góp ở dạng cặp trường-giá trị.
Xóa tất cả các phần tử bằng giá trị đã chỉ định
Trong ví dụ này, chúng tôi đang loại bỏ ngôn ngữ. “Java” và t Articles. 50 mục từ mảng tài liệu, tôi. e. , trường bài viết
Bạn có thể sử dụng toán tử $pull để xóa đối tượng khỏi một mảng trong MongoDB. Để hiểu khái niệm này, chúng ta hãy tạo một bộ sưu tập với tài liệu. Truy vấn để tạo một bộ sưu tập với một tài liệu như sau -
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }
Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find[]. Truy vấn như sau -
> db.removeObjectFromArrayDemo.find[].pretty[];
Sau đây là đầu ra -
{ "_id" : ObjectId["5c8ad13d6cea1f28b7aa0817"], "StudentName" : "John", "StudentAcademicProjectDetails" : [ { "StudentProjectId" : 101, "StudentProjectName" : "Pig Dice Game" }, { "StudentProjectId" : 110, "StudentProjectName" : "Library Management System" }, { "StudentProjectId" : 120, "StudentProjectName" : "Phonebook Management System" } ] }
Đây là truy vấn để xóa đối tượng khỏi một mảng trong MongoDB -
> db.removeObjectFromArrayDemo.update[ .. {'_id': ObjectId["5c8ad13d6cea1f28b7aa0817"]}, .. { $pull: { "StudentAcademicProjectDetails" : { StudentProjectId: 101 } } }, .. false, .. true .. ]; WriteResult[{ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }]
Hãy để chúng tôi kiểm tra các tài liệu từ bộ sưu tập để kiểm tra đối tượng đã bị xóa khỏi một mảng hay chưa. Truy vấn như sau -
Toán tử
db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
6 loại bỏ khỏi một mảng hiện có tất cả các phiên bản của một giá trị hoặc các giá trị khớp với một điều kiện đã chỉ địnhGiả sử chúng ta có một bộ sưu tập tên là
db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
7 với các tài liệu sau{ "_id" : 1, "prod" : "Bat", "sizes" : [ "S", "M", "XL", "XXL" ] } { "_id" : 2, "prod" : "Hat", "sizes" : [ "S", "L", "XL" ] } { "_id" : 3, "prod" : "Cap", "sizes" : [ "XS", "S", "M", "L", "XL" ] }
Chúng ta có thể xóa một phần tử khỏi mảng trong tài liệu 1 như thế này
db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
Bây giờ khi chúng tôi kiểm tra bộ sưu tập, chúng tôi có thể thấy rằng
db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
8 đã bị xóa khỏi tài liệu 1 theo quy định> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }0
Kết quả
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }1
Toán Tử db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
3
db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
Toán tử
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }00 xóa tất cả các phiên bản của các giá trị đã chỉ định khỏi mảng
Giả sử chúng ta có một bộ sưu tập với các tài liệu sau
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }4
Và chúng tôi muốn xóa tất cả các giá trị của
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }01 khỏi mảng trong tài liệu 1
Chung ta co thể lam được việc nay
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }6
Điều này loại bỏ cả hai lần xuất hiện của 7 khỏi mảng trong tài liệu 1
Chúng tôi có thể xác minh điều này bằng cách kiểm tra bộ sưu tập
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }7
Kết quả
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }8
Chúng ta có thể thấy rằng mảng trong tài liệu 1 đã bị xóa tất cả các giá trị
> db.removeObjectFromArrayDemo.insertOne[ .. { ... .. "StudentName": "John", .. "StudentAcademicProjectDetails": .. [{ .. "StudentProjectId": 101, .. "StudentProjectName": "Pig Dice Game" .. }, .. { .. "StudentProjectId": 110, .. "StudentProjectName": "Library Management System" .. }, ... .. { .. "StudentProjectId": 120, .. "StudentProjectName": "Phonebook Management System" .. } .. ] .. } .. ]; { "acknowledged" : true, "insertedId" : ObjectId["5c8ad13d6cea1f28b7aa0817"] }01 theo chỉ định
Mặc dù
db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
3 tương tự như db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
2, nhưng điểm khác biệt là db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
3 xóa các phần tử khớp với các giá trị được liệt kê. Điều này không giống như db.products.update[
{ _id: 1 },
{ $pull: { sizes: "XXL" } }
]
2, nơi bạn loại bỏ các giá trị bằng cách chỉ định một truy vấn