Computer >> Máy Tính >  >> Lập trình >> MongoDB

Làm cách nào để sử dụng MongoDB $ pull để xóa tài liệu trong Mảng?

Bạn cần sử dụng lệnh update cùng với toán tử $ pull để xóa tài liệu trong một mảng. Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu. Sau đây là truy vấn

> db.deleteDocumentsDemo.insertOne(
... {
...    "_id":100,
...    "StudentsDetails" : [
...       {
...          "StudentId" : 1,
...          "StudentName" : "John"
...       },
...       {
...          "StudentId" : 2,
...          "StudentName" : "Carol"
...       },
...       {
...          "StudentId" : 3,
...          "StudentName" : "Sam"
...       },
...       {
...          "StudentId" : 4,
...          "StudentName" : "Mike"
...       }
...    ]
... }
...
... );
{ "acknowledged" : true, "insertedId" : 100 }
> db.deleteDocumentsDemo.insertOne(
... {
...    "_id":200,
...    "StudentsDetails" : [
...       {
...          "StudentId" : 5,
...          "StudentName" : "David"
...       },
...       {
...          "StudentId" : 6,
...          "StudentName" : "Ramit"
...       },
...       {
...          "StudentId" : 7,
...          "StudentName" : "Adam"
...       },
...       {
...          "StudentId" : 8,
...          "StudentName" : "Larry"
...       }
...    ]
... }
...
... );
{ "acknowledged" : true, "insertedId" : 200 }

Sau đây là truy vấn để 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 ()

> db.deleteDocumentsDemo.find().pretty();

Điều này sẽ tạo ra kết quả sau

{
   "_id" : 100,
   "StudentsDetails" : [
      {
         "StudentId" : 1,
         "StudentName" : "John"
      },
      {
         "StudentId" : 2,
         "StudentName" : "Carol"
      },
      {
         "StudentId" : 3,
         "StudentName" : "Sam"
      },
      {
         "StudentId" : 4,
         "StudentName" : "Mike"
      }
   ]
}
{
   "_id" : 200,
   "StudentsDetails" : [
      {
         "StudentId" : 5,
         "StudentName" : "David"
      },
      {
         "StudentId" : 6,
         "StudentName" : "Ramit"
      },
      {
         "StudentId" : 7,
         "StudentName" : "Adam"
      },
      {
         "StudentId" : 8,
         "StudentName" : "Larry"
      }
   ]
}

Sau đây là truy vấn để xóa tài liệu trong một mảng

> db.deleteDocumentsDemo.update({},
... {$pull: {StudentsDetails: {StudentName: "David"}}},
... {multi: true});
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 })

Hãy để chúng tôi kiểm tra các tài liệu có bị xóa hay không. Sau đây là truy vấn

> db.deleteDocumentsDemo.find().pretty();

Điều này sẽ tạo ra kết quả sau

{
   "_id" : 100,
   "StudentsDetails" : [
      {
         "StudentId" : 1,
         "StudentName" : "John"
      },
      {
         "StudentId" : 2,
         "StudentName" : "Carol"
      },
      {
         "StudentId" : 3,
         "StudentName" : "Sam"
      },
      {
         "StudentId" : 4,
         "StudentName" : "Mike"
      }
   ]
}
{
   "_id" : 200,
   "StudentsDetails" : [
      {
         "StudentId" : 6,
         "StudentName" : "Ramit"
      },
      {
         "StudentId" : 7,
         "StudentName" : "Adam"
      },
      {
         "StudentId" : 8,
         "StudentName" : "Larry"
      }
   ]
}

Nhìn vào kết quả đầu ra mẫu ở trên, “StudentId” có giá trị 5 tức là StudentName “David” đã bị xóa.