Mongodb?刪除文檔Delete與Remove的區(qū)別解析
db.collection.remove() 此方法已被 mongosh 棄用
| 已棄用的方法 | 替代方法 |
|---|---|
| db.collection.remove() | db.collection.deleteOne()db.collection.deleteMany()db.collection.findOneAndDelete()db.collection.bulkWrite() |
5.0版本更改。
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>,
collation: <document>,
let: <document> // Added in MongoDB 5.0
}
)db.collection.remove() 方法可以具有兩種語法之一。
remove()方法可以采用查詢文檔和可選的 justOne 布爾值參數(shù)及操作方法區(qū)分
詳細(xì)參數(shù)區(qū)別請參考下面文檔:
刪除所有文檔
要從集合中刪除所有文檔,請將空過濾器文檔傳遞 {} 給該 db.collection.deleteMany()方法。
db.collection.deleteMany() 具有以下語法:
db.collection.deleteMany(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)以下示例從集合中刪除所有文檔 myCollection :
sit_rs1:PRIMARY> db.myCollection.find()
{ "_id" : 1, "a" : 1, "b" : 1 }
{ "_id" : 0, "a" : 1, "b" : 1 }
sit_rs1:PRIMARY> db.myCollection.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 2 }
sit_rs1:PRIMARY> db.myCollection.count()
0刪除所有符合條件的文檔
您可以指定用于標(biāo)識要刪除的文檔的條件或過濾器。過濾器使用與讀取操作相同的語法。
要指定相等條件,請在查詢過濾器文檔<field>:<value> 中使用表達(dá)式 :
{ <field1>: <value1>, ... }查詢過濾文檔可以使用查詢運(yùn)算符來指定條件,格式如下:
{ <field1>: { <operator1>: <value1> }, ... }要刪除與刪除條件匹配的所有文檔,請將篩選器參數(shù)傳遞給該 deleteMany()方法。
該方法返回包含操作狀態(tài)的文檔。
如下示例,刪除訂單表中 “cust_id” : “A” 的記錄,如下:
sit_rs1:PRIMARY> db.orders.find()
{ "_id" : 2, "cust_id" : "A", "ord_date" : ISODate("2023-06-08T00:00:00Z"), "price" : 60, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 }, { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.782Z") }
{ "_id" : 7, "cust_id" : "C", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 21, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 9, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 51, "items" : [ { "sku" : "carrots", "qty" : 5, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 }, { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 1, "cust_id" : "A", "ord_date" : ISODate("2023-06-01T00:00:00Z"), "price" : 15, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 }, { "sku" : "apples", "qty" : 5, "price" : 2.5 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.781Z") }
{ "_id" : 4, "cust_id" : "B", "ord_date" : ISODate("2023-06-18T00:00:00Z"), "price" : 26, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.782Z") }
{ "_id" : 10, "cust_id" : "D", "ord_date" : ISODate("2023-06-23T00:00:00Z"), "price" : 23, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 6, "cust_id" : "C", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 38, "items" : [ { "sku" : "carrots", "qty" : 10, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 5, "cust_id" : "B", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 40, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 8, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 76, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64dde9e72fabd7cc0b2c6faa"), "cust_id" : "E", "status" : "0", "price" : 1 }
sit_rs1:PRIMARY> db.orders.deleteMany({ "cust_id" : "A" })
{ "acknowledged" : true, "deletedCount" : 2 }
sit_rs1:PRIMARY> db.orders.find()
{ "_id" : 7, "cust_id" : "C", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 21, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 9, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 51, "items" : [ { "sku" : "carrots", "qty" : 5, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 }, { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 4, "cust_id" : "B", "ord_date" : ISODate("2023-06-18T00:00:00Z"), "price" : 26, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.782Z") }
{ "_id" : 10, "cust_id" : "D", "ord_date" : ISODate("2023-06-23T00:00:00Z"), "price" : 23, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 6, "cust_id" : "C", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 38, "items" : [ { "sku" : "carrots", "qty" : 10, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 5, "cust_id" : "B", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 40, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 8, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 76, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64dde9e72fabd7cc0b2c6faa"), "cust_id" : "E", "status" : "0", "price" : 1 }刪除單個文檔
要最多刪除與指定過濾器匹配的單個文檔(即使多個文檔可能與指定過濾器匹配),請使用該db.collection.deleteOne()方法。
即使從集合中刪除所有文檔,刪除操作也不會刪除索引。
db.collection.deleteOne() 具有以下語法:
db.collection.deleteOne(
<filter>,
{
writeConcern: <document>,
collation: <document>,
hint: <document|string> // Available starting in MongoDB 4.4
}
)以下示例刪除 “cust_id” : “B” 的訂單,只有 “_id” : 4 的記錄會被刪除 ?。。。。。。。?!
sit_rs1:PRIMARY> db.orders.find().sort({_id:1})
{ "_id" : 4, "cust_id" : "B", "ord_date" : ISODate("2023-06-18T00:00:00Z"), "price" : 26, "items" : [ { "sku" : "apple", "qty" : "15", "price" : 2.5 } ], "status" : "0", "lastModified" : ISODate("2023-08-11T09:50:49.782Z") }
{ "_id" : 5, "cust_id" : "B", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 40, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 6, "cust_id" : "C", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 38, "items" : [ { "sku" : "carrots", "qty" : 10, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 7, "cust_id" : "C", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 21, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 8, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 76, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 9, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 51, "items" : [ { "sku" : "carrots", "qty" : 5, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 }, { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 10, "cust_id" : "D", "ord_date" : ISODate("2023-06-23T00:00:00Z"), "price" : 23, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64dde9e72fabd7cc0b2c6faa"), "cust_id" : "E", "status" : "0", "price" : 1 }
sit_rs1:PRIMARY> db.orders.deleteOne({ "cust_id" : "B" })
{ "acknowledged" : true, "deletedCount" : 1 }
sit_rs1:PRIMARY> db.orders.find().sort({_id:1})
{ "_id" : 5, "cust_id" : "B", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 40, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 6, "cust_id" : "C", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 38, "items" : [ { "sku" : "carrots", "qty" : 10, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 7, "cust_id" : "C", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 21, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 8, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 76, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 9, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 51, "items" : [ { "sku" : "carrots", "qty" : 5, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 }, { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 10, "cust_id" : "D", "ord_date" : ISODate("2023-06-23T00:00:00Z"), "price" : 23, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64dde9e72fabd7cc0b2c6faa"), "cust_id" : "E", "status" : "0", "price" : 1 }db.collection.remove() 從集合中刪除文檔。
db.collection.remove()方法可以具有兩種語法之一。remove()方法可以采用查詢文檔和可選的justOne布爾值:
db.collection.remove( <query>, <justOne> )
或者該方法可以采用查詢文檔和可選的刪除選項(xiàng)文檔:
5.0版本更改。
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>,
collation: <document>,
let: <document> // Added in MongoDB 5.0
}
)從集合中刪除所有文檔。 要刪除集合中的所有文檔,請調(diào)用 remove 具有空查詢文檔的方法{}。以下操作將從 集合 myCollection 中刪除所有文檔:
此操作不等同于該 drop()方法。drop()要從集合中刪除所有文檔,使用該方法刪除整個集合(包括索引)。
sit_rs1:PRIMARY> db.myCollection.insertMany( [ { _id: 0, a: 1, b: 1 }, { _id: 1, a: 1, b: 1 } ] )
{ "acknowledged" : true, "insertedIds" : [ 0, 1 ] }
sit_rs1:PRIMARY> db.myCollection.find()
{ "_id" : 1, "a" : 1, "b" : 1 }
{ "_id" : 0, "a" : 1, "b" : 1 }
sit_rs1:PRIMARY> db.myCollection.remove({})
WriteResult({ "nRemoved" : 2 })
sit_rs1:PRIMARY> db.myCollection.find().count()
0刪除所有符合條件的文檔。要刪除符合刪除條件的文檔,請調(diào)用 remove() 方法參數(shù) <query>
以下操作從 myCollection 集合中刪除 a: 1 的所有文檔 :
sit_rs1:PRIMARY> db.myCollection.insertMany( [
... { _id: 0, a: 1, b: 1 },
... { _id: 1, a: 1, b: 2 },
... { _id: 2, a: 2, b: 3 },
... { _id: 3, a: 3, b: 3 },
... { _id: 4, a: 1, b: 4 },
... ] )
{ "acknowledged" : true, "insertedIds" : [ 0, 1, 2, 3, 4 ] }
sit_rs1:PRIMARY> db.myCollection.find()
{ "_id" : 1, "a" : 1, "b" : 2 }
{ "_id" : 0, "a" : 1, "b" : 1 }
{ "_id" : 2, "a" : 2, "b" : 3 }
{ "_id" : 3, "a" : 3, "b" : 3 }
{ "_id" : 4, "a" : 1, "b" : 4 }
sit_rs1:PRIMARY> db.myCollection.remove({ "a" : 1 })
WriteResult({ "nRemoved" : 3 })
sit_rs1:PRIMARY> db.myCollection.find()
{ "_id" : 2, "a" : 2, "b" : 3 }
{ "_id" : 3, "a" : 3, "b" : 3 }刪除與條件匹配的單個文檔,要刪除第一個符合刪除條件的文檔,請調(diào)用 remove方法,其query 條件和 justOne 參數(shù)設(shè)置為true或1。
以下操作從集合 myCollection 中刪除第一個 a 大于等于2 的文檔 :
sit_rs1:PRIMARY> db.myCollection.insertMany( [
... { _id: 0, a: 1, b: 1 },
... { _id: 1, a: 1, b: 2 },
... { _id: 2, a: 2, b: 3 },
... { _id: 3, a: 3, b: 3 },
... { _id: 4, a: 1, b: 4 },
... ] )
{ "acknowledged" : true, "insertedIds" : [ 0, 1, 2, 3, 4 ] }
sit_rs1:PRIMARY> db.myCollection.find()
{ "_id" : 1, "a" : 1, "b" : 2 }
{ "_id" : 0, "a" : 1, "b" : 1 }
{ "_id" : 2, "a" : 2, "b" : 3 }
{ "_id" : 3, "a" : 3, "b" : 3 }
{ "_id" : 4, "a" : 1, "b" : 4 }
sit_rs1:PRIMARY> db.myCollection.remove({ "a": { $gte: 2 } }, true )
WriteResult({ "nRemoved" : 1 })
sit_rs1:PRIMARY> db.myCollection.find()
{ "_id" : 1, "a" : 1, "b" : 2 }
{ "_id" : 0, "a" : 1, "b" : 1 }
{ "_id" : 3, "a" : 3, "b" : 3 }
{ "_id" : 4, "a" : 1, "b" : 4 }到此這篇關(guān)于Mongodb 刪除文檔Delete與Remove的區(qū)別的文章就介紹到這了,更多相關(guān)Mongodb Delete與Remove區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB中的常用操作$set、$unset和$inc示例詳解
在MongoDB中,$set操作符用于更新文檔中的字段值,它允許更新指定的字段,而不必更新整個文檔,這篇文章主要介紹了MongoDB中的常用操作$set、$unset和$inc示例詳解,需要的朋友可以參考下2023-12-12
MongoDB多數(shù)據(jù)源配置與切換的方法示例
這篇文章主要介紹了MongoDB多數(shù)據(jù)源配置與切換的方法示例,如何在SpringBoot應(yīng)用中配置并使用兩個MongoDB數(shù)據(jù)源,包括YAML配置文件的編寫,避免默認(rèn)MongoTemplate注入,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
Linux服務(wù)器快速安裝MongoDB5.0版本過程步驟
這篇文章主要為大家介紹了Linux服務(wù)器快速安裝MongoDB5.0版本過程步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
MongoDB入門教程之細(xì)說MongoDB數(shù)據(jù)庫的增刪查改操作
這篇文章主要介紹了MongoDB入門教程之細(xì)說MongoDB數(shù)據(jù)庫的增刪查改操作,本文環(huán)境是windows,所以以圖片形式講解,需要的朋友可以參考下2014-08-08
Mongodb中MapReduce實(shí)現(xiàn)數(shù)據(jù)聚合方法詳解
Mongodb是針對大數(shù)據(jù)量環(huán)境下誕生的用于保存大數(shù)據(jù)量的非關(guān)系型數(shù)據(jù)庫,針對大量的數(shù)據(jù)。接下來通過本文給大家介紹Mongodb中MapReduce實(shí)現(xiàn)數(shù)據(jù)聚合方法詳解,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
Windows或Linux系統(tǒng)中備份和恢復(fù)MongoDB數(shù)據(jù)的教程
不得不說MongoDB的備份回復(fù)操作對比其他數(shù)據(jù)庫來說真的算得上是簡便的,無論是在Windows的命令行中或者是Linux里的腳本執(zhí)行,這里我們就來看一下Windows或Linux系統(tǒng)中備份和恢復(fù)MongoDB數(shù)據(jù)的教程2016-06-06

