MongoDB学习第六篇 --- Delete操作
数据如下:
db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }, { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" }, { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }, { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }, { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }, ]);
一、shell操作delete语句
db.inventory.deleteMany({}) # 删除所有 document db.inventory.deleteMany({ status : "A" }) # 删除所有 匹配到document db.inventory.deleteOne( { status: "D" } ) # 只删除第一个匹配的 document
二、python操作delete语句
db.inventory.delete_many({}) db.inventory.delete_many({"status": "A"}) db.inventory.delete_one({"status": "D"})
三、java操作delete语句
collection.deleteMany(new Document()); collection.deleteMany(eq("status", "A")); collection.deleteOne(eq("status", "D"));