Mongodb 删除文档Delete与Remove的区别解析
作者:Ethanchen's notes
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 布尔值参数及操作方法区分
详细参数区别请参考下面文档:
删除所有文档
要从集合中删除所有文档,请将空过滤器文档传递 {} 给该 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> }, ... }
要删除与删除条件匹配的所有文档,请将筛选器参数传递给该 deleteMany()方法。
该方法返回包含操作状态的文档。
如下示例,删除订单表中 “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 } )
从集合中删除所有文档。 要删除集合中的所有文档,请调用 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
删除所有符合条件的文档。要删除符合删除条件的文档,请调用 remove() 方法参数 <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 }
删除与条件匹配的单个文档,要删除第一个符合删除条件的文档,请调用 remove方法,其query 条件和 justOne 参数设置为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 }
到此这篇关于Mongodb 删除文档Delete与Remove的区别的文章就介绍到这了,更多相关Mongodb Delete与Remove区别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!