db.collection.deleteMany()
คำสั่ง db.collection.deleteMany()
เป็นคำสั่งสำหรับ ลบข้อมูลทั้งหมด ที่เงื่อนไขที่กำหนด
อ้างอิงจาก ลิงค์นี้
ข้อมูลที่ Return
Section titled “ข้อมูลที่ Return”เมื่อใช้คำสั่งนี้จะข้อมูลที่ Return มาจะมี
{ "acknowledged" : boolean, "deletedCount": number}
acknowledged
มีค่าเป็น BooleandeletedCount
มีค่าเป็นnumber
ระบุว่าค้นหาข้อมูลที่เจอทั้งหมดเท่าไหร่
Syntax
Section titled “Syntax”db.collection.deleteMany( <filter>, { writeConcern: <document>, collation: <document>, hint: <document>|<string> })
Parameter | ชนิดข้อมูล | คำอธิบาย |
---|---|---|
filter | document | กำหนดเงื่อนไขว่า จะให้อ่าน document ที่ตรงกับเงื่อนไข มาลบ |
writeConcern | document | (Optional) - สามารถดูการตั้งค่า เพิ่มเติมในนี้ได้ครับ |
collation | document | (Optional) - ระบุข้อมูลที่เกี่ยวข้องทางภาษา เพื่อใช้ในการเปรียบเทียบข้อมูลในขณะค้นหาเช่นภาษาไทย ตัวเล็กตัวใหญ่ |
hint | document/string | (Optional) - ระบุชื่อของ index ว่าตอน filter ข้อมูลมา update นั้นให้เรียกใช้ index นี้ |
Example
Section titled “Example”เพื่อความเข้าใจเรามาดูการใช้งานกันดีกว่าครับ
มีข้อมูลใน collection ดังนี้
[ { "name": "Person name 1", "position": "Developer", "age": 25, "education": [ { 2021: "Diploma" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-01-01") }, { "name": "Person name 2", "position": "Developer", "age": 26, "education": [ { 2021: "Vocational school" }, { 2022: "Bachelor" } ], "createdby": ISODate("2023-02-01") }, { "name": "Person name 3", "position": "Senior Developer", "age": 30, "education": [ { 2021: "Bachelor" }, { 2022: "Master" } ], "createdby": ISODate("2023-02-03") }]
ต้องการลบข้อมูลที่ age
มากกว่า 27
db.collection.deleteMany({"age": { "$gte": 27 }})
จะได้ response กลับมา
{ acknowledged: true, deletedCount: 1}