不管是数据库还是操作系统的文件,进行删除操作需要谨慎,确认确认再确认。 MongoDB提供以下方法来删除集合中的文档delete、drop、remove。drop操作会删除集合及索引文件。
1、remove 删除单个文档或匹配指定筛选器的所有文档。
rs02:PRIMARY> db.mycoll.find()
{ "_id" : 1, "category" : "café", "status" : "A" }
{ "_id" : 2, "category" : "cafe", "status" : "a" }
{ "_id" : 3, "category" : "cafE", "status" : "a" }
rs02:PRIMARY> db.mycoll.remove({"status" : "A"}) //删除集合中"status" 值等于"A"的文档
WriteResult({ "nRemoved" : 1 })
rs02:PRIMARY> db.mycoll.find()
{ "_id" : 2, "category" : "cafe", "status" : "a" }
{ "_id" : 3, "category" : "cafE", "status" : "a" }
rs02:PRIMARY> db.mycoll.remove({}) //删除集合中的所有文档
WriteResult({ "nRemoved" : 2 })
rs02:PRIMARY> db.mycoll.find()
rs02:PRIMARY> db.mycoll.getIndexes() //索引还在
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test_jia.mycoll"
}
]
rs02:PRIMARY>
2、使用命令deleteOne和deleteMany删除文档
rs02:PRIMARY> db.inventory.find()
{ "_id" : ObjectId("6386a8ab9011e974ab4118f2"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("6386a8ab9011e974ab4118f3"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "P" }
{ "_id" : ObjectId("6386a8ab9011e974ab4118f4"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("6386a8ab9011e974ab4118f5"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("6386a8ab9011e974ab4118f6"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
rs02:PRIMARY> db.inventory.getIndexes() //查看索引
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test_jia.inventory"
}
]
2.1、deleteOne删除文档
rs02:PRIMARY> db.inventory.deleteOne( { status: "D" } ) //deleteOne删除文档删除条件是status值为 "D"
{ "acknowledged" : true, "deletedCount" : 1 }
rs02:PRIMARY> db.inventory.find() //查看数据
{ "_id" : ObjectId("6386a8ab9011e974ab4118f2"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("6386a8ab9011e974ab4118f3"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "P" }
{ "_id" : ObjectId("6386a8ab9011e974ab4118f5"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("6386a8ab9011e974ab4118f6"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
2.2、deleteMany删除文档
rs02:PRIMARY> db.inventory.deleteMany({ status : "A" }) //deleteMany删除符合status值为 "A"
{ "acknowledged" : true, "deletedCount" : 2 }
rs02:PRIMARY> db.inventory.find() //查看数据
{ "_id" : ObjectId("6386a8ab9011e974ab4118f3"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "P" }
{ "_id" : ObjectId("6386a8ab9011e974ab4118f5"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
rs02:PRIMARY> db.inventory.deleteMany({}) //删除集合中所有文档
{ "acknowledged" : true, "deletedCount" : 2 }
rs02:PRIMARY> db.inventory.find()
rs02:PRIMARY> db.inventory.getIndexes() //索引还在
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test_jia.inventory"
}
3、drop删除集合
rs02:PRIMARY> db.inventory.drop() //删除集合包括索引文件
true
rs02:PRIMARY> db.inventory.find()
rs02:PRIMARY>
3、drop数据库
db.dropDatabase() 删除当前数据库,删除相关的数据文件,
rs02:PRIMARY> use user_info
switched to db user_info
rs02:PRIMARY> show tables;
ip
test1
rs02:PRIMARY> db.dropDatabase()
{
"dropped" : "user_info",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1669790100, 3),
"signature" : {
"hash" : BinData(0,"A8ZFmKSlf9XsyZXbwpZ2UX6Q6vo="),
"keyId" : NumberLong("7152775128156209153")
}
},
"operationTime" : Timestamp(1669790100, 3)
}
rs02:PRIMARY>
标签:status,删除,Mongodb,rs02,db,PRIMARY,inventory,操作,id
From: https://blog.51cto.com/jiachen/5899848