Mongodb 删除文档Delete与Remove的区别

db.collection.remove() 此方法已被 mongosh 弃用

已弃用的方法 替代方法
db.collection.remove() db.collection.deleteOne() db.collection.deleteMany() db.collection.findOneAndDelete() db.collection.bulkWrite()

5.0版本更改。

js 复制代码
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() 具有以下语法:

js 复制代码
db.collection.deleteMany(
   <filter>,
   {
      writeConcern: <document>,
      collation: <document>
   }
)

以下示例从集合中删除所有文档 myCollection :

js 复制代码
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> 中使用表达式 :

shell 复制代码
{ <field1>: <value1>, ... }

查询过滤文档可以使用查询运算符来指定条件,格式如下:

shell 复制代码
{ <field1>: { <operator1>: <value1> }, ... }

要删除与删除条件匹配的所有文档,请将筛选器参数传递给该 deleteMany()方法。

该方法返回包含操作状态的文档。

如下示例,删除订单表中 "cust_id" : "A" 的记录,如下:

js 复制代码
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() 具有以下语法:

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

以下示例删除 "cust_id" : "B" 的订单,只有 "_id" : 4 的记录会被删除 !!!!!!!!!

js 复制代码
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布尔值:

js 复制代码
db.collection.remove(
   <query>,
   <justOne>
)

或者该方法可以采用查询文档和可选的删除选项文档:

5.0版本更改。

js 复制代码
db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     let: <document> // Added in MongoDB 5.0
   }
)

从集合中删除所有文档。 要删除集合中的所有文档,请调用 remove 具有空查询文档的方法{}。以下操作将从 集合 myCollection 中删除所有文档:

此操作不等同于该 drop()方法。drop()要从集合中删除所有文档,使用该方法删除整个集合(包括索引)。

js 复制代码
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 的所有文档 :

js 复制代码
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 的文档 :

js 复制代码
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 }
相关推荐
The_Ticker14 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
Elastic 中国社区官方博客20 分钟前
Elasticsearch 开放推理 API 增加了对 IBM watsonx.ai Slate 嵌入模型的支持
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
企鹅侠客25 分钟前
ETCD调优
数据库·etcd
Json_1817901448031 分钟前
电商拍立淘按图搜索API接口系列,文档说明参考
前端·数据库
煎饼小狗42 分钟前
Redis五大基本类型——Zset有序集合命令详解(命令用法详解+思维导图详解)
数据库·redis·缓存
永乐春秋1 小时前
WEB-通用漏洞&SQL注入&CTF&二次&堆叠&DNS带外
数据库·sql
打鱼又晒网2 小时前
【MySQL】数据库精细化讲解:内置函数知识穿透与深度学习解析
数据库·mysql
大白要努力!2 小时前
android 使用SQLiteOpenHelper 如何优化数据库的性能
android·数据库·oracle
tatasix2 小时前
MySQL UPDATE语句执行链路解析
数据库·mysql
南城花随雪。3 小时前
硬盘(HDD)与固态硬盘(SSD)详细解读
数据库