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区别内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论