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

Làm cách nào để xóa tài liệu trong MongoDB?

Để xóa một tài liệu, hãy sử dụng remove (). Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -

> db.demo79.insertOne ({"Name":"Chris"}); {"nition ":true," insertId ":ObjectId (" 5e2bdb2271bf0181ecc42293 ")}> db.demo79.insertOne ({" Name " :"David"}); {"Recognition":true, "insertId":ObjectId ("5e2bdb2671bf0181ecc42294")}> db.demo79.insertOne ({"Name":"Bob"}); {"Recog":true, "insertId":ObjectId ("5e2bdb2971bf0181ecc42295")}> db.demo79.insertOne ({"Name":"Mike"}); {"Recog":true, "insertId":ObjectId ("5e2bdb2c71bf0181ecc42296")}> 

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.demo79.find (); 

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

 {"_id":ObjectId ("5e2bdb2271bf0181ecc42293"), "Tên":"Chris"} {"_id":ObjectId ("5e2bdb2671bf0181ecc42294"), "Tên":"David"} {"_id":ObjectId ( "5e2bdb2971bf0181ecc42295"), "Tên":"Bob"} {"_id":ObjectId ("5e2bdb2c71bf0181ecc42296"), "Tên":"Mike"} 

Sau đây là truy vấn xóa tài liệu trong MongoDB -

> db.demo79.remove ({"Name":"Bob"}); WriteResult ({"nRemoved":1}) 

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.demo79.find (); 

Điều này sẽ tạo ra kết quả sau. Chúng tôi đã xóa tài liệu thành công -

 {"_id":ObjectId ("5e2bdb2271bf0181ecc42293"), "Tên":"Chris"} {"_id":ObjectId ("5e2bdb2671bf0181ecc42294"), "Tên":"David"} {"_id":ObjectId ( "5e2bdb2c71bf0181ecc42296"), "Tên":"Mike"}