Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

จะใช้ MongoDB $pull เพื่อลบเอกสารภายใน Array ได้อย่างไร?


คุณต้องใช้คำสั่ง update พร้อมกับตัวดำเนินการ $pull เพื่อลบเอกสารภายในอาร์เรย์ ให้เราสร้างคอลเลกชันที่มีเอกสาร ต่อไปนี้เป็นแบบสอบถาม

> 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 }

ต่อไปนี้เป็นแบบสอบถามเพื่อแสดงเอกสารทั้งหมดจากคอลเลกชันโดยใช้วิธี find()

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

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้

{
   "_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"
      }
   ]
}

ต่อไปนี้เป็นแบบสอบถามเพื่อลบเอกสารภายในอาร์เรย์

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

ให้เราตรวจสอบเอกสารว่าถูกลบหรือไม่ ต่อไปนี้เป็นแบบสอบถาม

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

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้

{
   "_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"
      }
   ]
}

ดูตัวอย่างผลลัพธ์ด้านบน “StudentId” ที่มีค่า 5 เช่น StudentName “David” ถูกลบแล้ว