欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mongodb?刪除文檔Delete與Remove的區(qū)別解析

 更新時(shí)間:2023年08月23日 10:12:06   作者:Ethanchen's?notes  
這篇文章主要介紹了Mongodb?刪除文檔Delete與Remove的區(qū)別,要從集合中刪除所有文檔,請(qǐng)將空過(guò)濾器文檔傳遞{}給該?db.collection.deleteMany()方法,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

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() 方法可以具有兩種語(yǔ)法之一。

remove()方法可以采用查詢文檔和可選的 justOne 布爾值參數(shù)及操作方法區(qū)分

詳細(xì)參數(shù)區(qū)別請(qǐng)參考下面文檔:

刪除所有文檔

要從集合中刪除所有文檔,請(qǐng)將空過(guò)濾器文檔傳遞 {} 給該 db.collection.deleteMany()方法。

db.collection.deleteMany() 具有以下語(yǔ)法:

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)識(shí)要?jiǎng)h除的文檔的條件或過(guò)濾器。過(guò)濾器使用與讀取操作相同的語(yǔ)法。

要指定相等條件,請(qǐng)?jiān)诓樵冞^(guò)濾器文檔<field>:<value> 中使用表達(dá)式 :

{ <field1>: <value1>, ... }

查詢過(guò)濾文檔可以使用查詢運(yùn)算符來(lái)指定條件,格式如下:

{ <field1>: { <operator1>: <value1> }, ... }

要?jiǎng)h除與刪除條件匹配的所有文檔,請(qǐng)將篩選器參數(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 }

刪除單個(gè)文檔

要最多刪除與指定過(guò)濾器匹配的單個(gè)文檔(即使多個(gè)文檔可能與指定過(guò)濾器匹配),請(qǐng)使用該db.collection.deleteOne()方法。

即使從集合中刪除所有文檔,刪除操作也不會(huì)刪除索引。

db.collection.deleteOne() 具有以下語(yǔ)法:

db.collection.deleteOne(
   <filter>,
   {
      writeConcern: <document>,
      collation: <document>,
      hint: <document|string>        // Available starting in MongoDB 4.4
   }
)

以下示例刪除 “cust_id” : “B” 的訂單,只有 “_id” : 4 的記錄會(huì)被刪除 ?。。。。。。。?!

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()方法可以具有兩種語(yǔ)法之一。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
   }
)

從集合中刪除所有文檔。 要?jiǎng)h除集合中的所有文檔,請(qǐng)調(diào)用 remove 具有空查詢文檔的方法{}。以下操作將從 集合 myCollection 中刪除所有文檔:

此操作不等同于該 drop()方法。drop()要從集合中刪除所有文檔,使用該方法刪除整個(gè)集合(包括索引)。

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

刪除所有符合條件的文檔。要?jiǎng)h除符合刪除條件的文檔,請(qǐng)調(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 }

刪除與條件匹配的單個(gè)文檔,要?jiǎng)h除第一個(gè)符合刪除條件的文檔,請(qǐng)調(diào)用 remove方法,其query 條件和 justOne 參數(shù)設(shè)置為true或1。

以下操作從集合 myCollection 中刪除第一個(gè) 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MongoDB入門教程之Windows下的MongoDB數(shù)據(jù)庫(kù)安裝圖解

    MongoDB入門教程之Windows下的MongoDB數(shù)據(jù)庫(kù)安裝圖解

    這篇文章主要介紹了MongoDB入門教程之Windows下的MongoDB數(shù)據(jù)庫(kù)安裝圖解,本文還講解了MongoDB的基本操作,如insert、find、 update、remove等操作,需要的朋友可以參考下
    2014-08-08
  • mongodb使用docker搭建replicaSet集群與變更監(jiān)聽(tīng)(最新推薦)

    mongodb使用docker搭建replicaSet集群與變更監(jiān)聽(tīng)(最新推薦)

    replicaSet和cluster從部署難度相比,replicaSet要簡(jiǎn)單許多。如果所存儲(chǔ)的數(shù)據(jù)量規(guī)模不算太大的情況下,那么使用replicaSet方式部署mongodb是一個(gè)不錯(cuò)的選擇,這篇文章主要介紹了mongodb使用docker搭建replicaSet集群與變更監(jiān)聽(tīng),需要的朋友可以參考下
    2023-03-03
  • MongoDB TTL索引的實(shí)例詳解

    MongoDB TTL索引的實(shí)例詳解

    這篇文章主要介紹了 MongoDB TTL索引的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • MongoDB聚合功能淺析

    MongoDB聚合功能淺析

    這篇文章主要介紹了MongoDB聚合功能,需要的朋友可以參考下
    2014-07-07
  • window平臺(tái)安裝MongoDB數(shù)據(jù)庫(kù)圖文詳解

    window平臺(tái)安裝MongoDB數(shù)據(jù)庫(kù)圖文詳解

    本篇文章主要介紹了window平臺(tái)安裝MongoDB數(shù)據(jù)庫(kù)圖文詳解,主要介紹window下面安裝mogod的步驟和使用細(xì)節(jié)。感興趣的小伙伴們可以參考一下。
    2016-11-11
  • 關(guān)于mongodb版本升級(jí)問(wèn)題

    關(guān)于mongodb版本升級(jí)問(wèn)題

    這篇文章主要介紹了關(guān)于mongodb版本升級(jí)問(wèn)題,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 28個(gè)MongoDB經(jīng)典面試題詳解

    28個(gè)MongoDB經(jīng)典面試題詳解

    這篇文章主要介紹了28個(gè)MongoDB經(jīng)典面試題詳解,需要的朋友可以參考下
    2020-02-02
  • 淺談MongoDB內(nèi)部的存儲(chǔ)原理

    淺談MongoDB內(nèi)部的存儲(chǔ)原理

    這篇文章主要介紹了淺談MongoDB內(nèi)部的存儲(chǔ)原理,MongoDB是一個(gè)面向文檔的數(shù)據(jù)庫(kù)系統(tǒng)。使用C++編寫,不支持SQL,但有自己功能強(qiáng)大的查詢語(yǔ)法,需要的朋友可以參考下
    2023-07-07
  • MongoDB的索引

    MongoDB的索引

    數(shù)據(jù)庫(kù)中的索引就是用來(lái)提高查詢操作的性能,但是會(huì)影響插入、更新和刪除的效率,因?yàn)閿?shù)據(jù)庫(kù)不僅要執(zhí)行這些操作,還要負(fù)責(zé)索引的更新
    2017-05-05
  • Java操作MongoDB數(shù)據(jù)庫(kù)方法詳解

    Java操作MongoDB數(shù)據(jù)庫(kù)方法詳解

    本文給大家分享的是使用Java操作MongoDB的一些基本方法,包含多種數(shù)據(jù)庫(kù)的連接方式,增刪改查等方法,非常的實(shí)用,有需要的小伙伴可以參考下
    2018-01-01

最新評(píng)論