最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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ū)分

詳細參數(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

刪除所有符合條件的文檔

您可以指定用于標識要刪除的文檔的條件或過濾器。過濾器使用與讀取操作相同的語法。

要指定相等條件,請在查詢過濾器文檔<field>:<value> 中使用表達式 :

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

查詢過濾文檔可以使用查詢運算符來指定條件,格式如下:

{ <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>
)

或者該方法可以采用查詢文檔和可選的刪除選項文檔:

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)文章

  • ubuntu mongodb安裝在哪個文件夾路徑詳解

    ubuntu mongodb安裝在哪個文件夾路徑詳解

    這篇文章主要為大家介紹了ubuntu mongodb安裝在哪個文件夾的安裝路徑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • MongoDB中的常用操作$set、$unset和$inc示例詳解

    MongoDB中的常用操作$set、$unset和$inc示例詳解

    在MongoDB中,$set操作符用于更新文檔中的字段值,它允許更新指定的字段,而不必更新整個文檔,這篇文章主要介紹了MongoDB中的常用操作$set、$unset和$inc示例詳解,需要的朋友可以參考下
    2023-12-12
  • MongoDB 模式設(shè)計詳解

    MongoDB 模式設(shè)計詳解

    這篇文章主要介紹了MongoDB 模式設(shè)計詳解的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • MongoDB多數(shù)據(jù)源配置與切換的方法示例

    MongoDB多數(shù)據(jù)源配置與切換的方法示例

    這篇文章主要介紹了MongoDB多數(shù)據(jù)源配置與切換的方法示例,如何在SpringBoot應(yīng)用中配置并使用兩個MongoDB數(shù)據(jù)源,包括YAML配置文件的編寫,避免默認MongoTemplate注入,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2024-07-07
  • Linux服務(wù)器快速安裝MongoDB5.0版本過程步驟

    Linux服務(wù)器快速安裝MongoDB5.0版本過程步驟

    這篇文章主要為大家介紹了Linux服務(wù)器快速安裝MongoDB5.0版本過程步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • MongoDB磁盤IO問題的3種解決方法

    MongoDB磁盤IO問題的3種解決方法

    磁盤IO是不可避免的,除去減少或延緩磁盤操作,也需要盡量的增強磁盤IO性能和吞吐量。下面這篇文章主要給大家介紹了關(guān)于MongoDB磁盤IO問題的3種解決方法,需要的朋友可以參考借鑒,需要的朋友們下面來一起看看吧
    2018-07-07
  • MongoDB入門教程之細說MongoDB數(shù)據(jù)庫的增刪查改操作

    MongoDB入門教程之細說MongoDB數(shù)據(jù)庫的增刪查改操作

    這篇文章主要介紹了MongoDB入門教程之細說MongoDB數(shù)據(jù)庫的增刪查改操作,本文環(huán)境是windows,所以以圖片形式講解,需要的朋友可以參考下
    2014-08-08
  • Mongodb中MapReduce實現(xiàn)數(shù)據(jù)聚合方法詳解

    Mongodb中MapReduce實現(xiàn)數(shù)據(jù)聚合方法詳解

    Mongodb是針對大數(shù)據(jù)量環(huán)境下誕生的用于保存大數(shù)據(jù)量的非關(guān)系型數(shù)據(jù)庫,針對大量的數(shù)據(jù)。接下來通過本文給大家介紹Mongodb中MapReduce實現(xiàn)數(shù)據(jù)聚合方法詳解,感興趣的朋友一起學(xué)習(xí)吧
    2016-05-05
  • Windows或Linux系統(tǒng)中備份和恢復(fù)MongoDB數(shù)據(jù)的教程

    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
  • Mongodb如何開啟用戶訪問控制詳解

    Mongodb如何開啟用戶訪問控制詳解

    默認啟動 MongoDB 服務(wù)時沒有任何參數(shù),可以對數(shù)據(jù)庫任意操 作,而且可以遠程訪問數(shù)據(jù)庫,所以推薦開發(fā)階段可以不設(shè)置任何參數(shù),但對于生產(chǎn)環(huán)境還是要仔細考慮一下安全方面的因素,下面就介紹了Mongodb開啟用戶訪問控制的相關(guān)資料。
    2017-01-01

最新評論

汾阳市| 湾仔区| 隆化县| 金昌市| 邵东县| 汉中市| 韶关市| 马山县| 织金县| 泰宁县| 明水县| 汉阴县| 蒙自县| 兴义市| 尚义县| 湘潭县| 雅江县| 玉溪市| 嘉兴市| 罗田县| 平山县| 青冈县| 三明市| 会同县| 秦安县| 靖宇县| 通道| 凤庆县| 习水县| 永登县| 柞水县| 山阴县| 五常市| 惠水县| 浦县| 峡江县| 布拖县| 阿拉善右旗| 景宁| 四平市| 洞口县|