mongodb的刪除語句


轉自:http://www.mongoing.com/docs/tutorial/remove-documents.html

刪除的方法

MongoDB provides the following methods to delete documents of a collection:

db.collection.remove() Delete a single document or all documents that match a specified filter.
db.collection.deleteOne()

Delete at most a single document that match a specified filter even though multiple documents may match the specified filter.

3.2 新版功能.

db.collection.deleteMany()

刪除所有匹配指定過濾條件的文檔.

3.2 新版功能.

你可以指定條件或過濾器來找到要刪除的文檔.這些 filters 使用與讀操作相同的語法:

  • query filter document 能夠用 <field>:<value> 表達式指定相等條件並以此選出所有包含指定 <value> 的 <field> 的文檔:

    { <field1>: <value1>, ... } 
  • query filter document can use the query operators to specify conditions in the following form:

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

刪除的行為表現

索引

Delete operations do not drop indexes, even if deleting all documents from a collection.

原子性

MongoDB中所有的寫操作在單一文檔層級上是原子的.更多關於MongoDB和原子性的信息,請參見 原子性和事務處理.

示例集合

本頁提供了在 mongo shell中刪除操作的例子.在:program:mongo shell中運行如下命令以向例子中涉及到的 users 集合填入數據:

注解

如果 users 集合中已經包含了相同 _id 值的文檔,你需要在插入示例文檔前 drop 該集合( db.users.drop() ).

db.users.insertMany( [ { _id: 1, name: "sue", age: 19, type: 1, status: "P", favorites: { artist: "Picasso", food: "pizza" }, finished: [ 17, 3 ], badges: [ "blue", "black" ], points: [ { points: 85, bonus: 20 }, { points: 85, bonus: 10 } ] }, { _id: 2, name: "bob", age: 42, type: 1, status: "A", favorites: { artist: "Miro", food: "meringue" }, finished: [ 11, 25 ], badges: [ "green" ], points: [ { points: 85, bonus: 20 }, { points: 64, bonus: 12 } ] }, { _id: 3, name: "ahn", age: 22, type: 2, status: "A", favorites: { artist: "Cassatt", food: "cake" }, finished: [ 6 ], badges: [ "blue", "red" ], points: [ { points: 81, bonus: 8 }, { points: 55, bonus: 20 } ] }, { _id: 4, name: "xi", age: 34, type: 2, status: "D", favorites: { artist: "Chagall", food: "chocolate" }, finished: [ 5, 11 ], badges: [ "red", "black" ], points: [ { points: 53, bonus: 15 }, { points: 51, bonus: 15 } ] }, { _id: 5, name: "xyz", age: 23, type: 2, status: "D", favorites: { artist: "Noguchi", food: "nougat" }, finished: [ 14, 6 ], badges: [ "orange" ], points: [ { points: 71, bonus: 20 } ] }, { _id: 6, name: "abc", age: 43, type: 1, status: "A", favorites: { food: "pizza", artist: "Picasso" }, finished: [ 18, 12 ], badges: [ "black", "blue" ], points: [ { points: 78, bonus: 8 }, { points: 57, bonus: 7 } ] } ] ) 

刪除所有文檔

要想從集合中刪除所有文檔,傳遞一個空的 filter document {} 給 db.collection.deleteMany() 或 db.collection.remove() 方法.

db.collection.deleteMany()

如下的例子使用 db.collection.deleteMany() 方法從 users 集合中刪除了 所有 文檔:

db.users.deleteMany({}) 

該方法返回了操作狀態的文檔:

{ "acknowledged" : true, "deletedCount" : 7 } 

For more information and examples, see db.collection.deleteMany().

db.collection.remove()

作為另一種過選擇,如下的例子使用了 db.collection.remove() 方法從 users 集合中刪除 所有 文檔:

db.users.remove({}) 

要想從一個集合里刪除所有文檔,使用 drop() 方法刪除整個集合,包括索引然后重建該集合和索引或許會更高效.

刪除滿足條件的所有文檔

要想刪除匹配刪除條件的所有文檔,傳遞 filter 參數給 db.collection.deleteMany() 方法或 db.collection.remove() 方法.

db.collection.deleteMany()

如下例子使用 db.collection.deleteMany() 從 users 集合中刪除所有 status 字段等於 "A" 的文檔:

db.users.deleteMany({ status : "A" }) 

該方法返回了操作狀態的文檔:

{ "acknowledged" : true, "deletedCount" : 3 } 

db.collection.remove()

作為另一種選擇如下例子使用 db.collection.remove() 從 users 集合中刪除所有 status 字段等於 "A" 的文檔:

db.users.remove( { status : "P" } ) 

對於大量的刪除操作,把你想要保留的文檔復制到一個新的集合然后使用 db.collection.drop() 方法刪除原集合或許會更高效。

僅刪除一個滿足條件的文檔

要想最多刪除一個滿足指定過濾條件的文檔(即使多個文檔可以滿足該指定過濾條件),使用 db.collection.deleteOne() 方法或使用 db.collection.remove() 方法並將 <justOne> 參數設置為 true 或 1.

db.collection.deleteOne()

如下例子使用 db.collection.deleteOne() 刪除 第一個 status 字段等於 "A" 的文檔:

db.users.deleteOne( { status: "D" } ) 

db.collection.remove()

作為另一種選擇,如下例子使用 db.collection.remove() 並將 <justOne> 參數設置為``1`` 來刪除 第一個 status 字段等於 "A" 的文檔:

db.users.remove( { status: "D" }, 1) 

參見

其他方法

其他方法

如下方法也可以從集合中刪除文檔:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM