Delete Methods

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

Method Description
 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.

New in version 3.2.

 db.collection.deleteMany() 

Delete all documents that match a specified filter.

New in version 3.2.

You can specify criteria, or filters, that identify the documents to delete. These filters use the same syntax as read operations:

  • query filter document can specify equality condition with <field>:<value> expressions to select all documents that contain the <field> with the specified <value>:

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

Delete Behavior

Indexes

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

Atomicity

All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.

Example Collection

This page provides examples of remove operations in the mongo shell. To populate the users collection referenced in the examples, run the following in mongo shell:

NOTE: If the users collection already contains documents with the same _id values, you need to drop the collection (db.users.drop()) before inserting the example documents.

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 }
]
}
]
)

Delete All Documents

To remove all documents from a collection, pass an empty filter document {} to either thedb.collection.deleteMany() or the db.collection.remove() method.

db.collection.deleteMany()

The following example uses the db.collection.deleteMany() method to delete all documents from the users collection:

db.users.deleteMany({})

The method returns a document with the status of the operation:

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

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

db.collection.remove()

Alternatively, the following example uses the db.collection.remove() method to delete all documents from the users collection:

db.users.remove({})

To delete all documents from a collection, it may be more efficient to use the db.collection.drop() method to drop the entire collection, including the indexes, and then recreate the collection and rebuild the indexes.

Delete All Documents that Match a Condition

To delete all documents that match a deletion criteria, pass a filter parameter to eitherdb.collection.deleteMany() method or the db.collection.remove() method.

db.collection.deleteMany()

The following example uses db.collection.deleteMany() to remove all documents from the userscollection where the status field equals "A":

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

The method returns a document with the status of the operation:

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

db.collection.remove()

Alternatively, the following example uses db.collection.remove() to remove all documents from theusers collection where the status field equals "P":

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

For large deletion operations, it may be more efficient to copy the documents that you want to keep to a new collection and then use db.collection.drop() on the original collection.

Remove Only One Document that Matches a Condition

To delete at most a single document that match a specified filter,even though multiple documents may match the specified filter, use either the db.collection.deleteOne() method or the db.collection.remove() method with the <justOne> parameter set to true or 1.

db.collection.deleteOne()

The following example uses db.collection.deleteOne() to delete the first document where statusis "D".

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

db.collection.remove()

Alternatively, the following example uses the db.collection.remove() with the <justOne> parameter set to 1 to delete the first document where status is "D":

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

Additional Methods

The following methods can also delete documents from a collection:

Write Acknowledgement

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

MongoDB - MongoDB CRUD Operations, Delete Documents的更多相关文章

  1. MongoDB - MongoDB CRUD Operations, Insert Documents

    MongoDB provides the following methods for inserting documents into a collection: db.collection.inse ...

  2. MongoDB - MongoDB CRUD Operations, Update Documents

    Update Methods MongoDB provides the following methods for updating documents in a collection: Method ...

  3. MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell

    The db.collection.find() method returns a cursor. To access the documents, you need to iterate the c ...

  4. MongoDB - MongoDB CRUD Operations, Query Documents, Project Fields to Return from Query

    By default, queries in MongoDB return all fields in matching documents. To limit the amount of data ...

  5. MongoDB - MongoDB CRUD Operations, Query Documents

    Query Method MongoDB provides the db.collection.find() method to read documents from a collection. T ...

  6. MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields

    Different query operators in MongoDB treat null values differently. The examples on this page use th ...

  7. MongoDB - MongoDB CRUD Operations

    CRUD operations create, read, update, and delete documents. Create Operations Create or insert opera ...

  8. MongoDB - MongoDB CRUD Operations, Bulk Write Operations

    Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...

  9. Mongodb系列- CRUD操作介绍

    ---恢复内容开始--- 一 Create 操作 在MongoDB中,插入操作的目标是一个集合. MongoDB中的所有写入操作在单个文档的层次上都是原子的. For examples, see In ...

随机推荐

  1. 项目冲刺Beta第三篇博客

    Beta版本冲刺计划安排 1.当天站立式会议照片: 2.工作分工: 团队成员 分工 张洪滨060  排行榜界面美化 陈敬轩059  注册成功界面美化 黄兴067  登录界面美化 林国梽068  答题界 ...

  2. Java 线程结束 & 守护线程

    /* 停止线程: 1,stop方法. 2,run方法结束. 怎么控制线程的任务结束呢? 任务中都会有循环结构,只要控制住循环就可以结束任务. 控制循环通常就用定义标记来完成. 但是如果线程处于了冻结状 ...

  3. Hibernate(十)

    1.批处理 //批处理 :批量处理 //批量插入数据 @Test public void addData(){ Random random=new Random(); for(int i=1;i< ...

  4. 使用flex布局调换两个按钮的位置

    组件用的时antd的Modal组件,里面的按钮需要调换一下位置 今天发现用flex布局非常方便,代码如下: display: flex; justify-content: center; flex-f ...

  5. OpenSSL 自签名证书

    通过下面9步,可以轻松生成自签名证书. 1.安装.部署OpenSSL 略 2.创建文件夹(下面通常root文件夹).用来放即将创建的各种证书等.如:I:\Key10.167.219.64 3.在roo ...

  6. hbase 跳转过滤器skipfilter

    用于跳过整个行键,需要和其他过滤器一起使用,本例SkipFilter和ValueFilter过滤器组合使用过滤不符合条件的行, 如果不配合SkipFiter,ValueFilter只过滤单元值包含的列 ...

  7. Go 示例测试实现原理剖析

    简介 示例测试相对于单元测试和性能测试来说,其实现机制比较简单.它没有复杂的数据结构,也不需要额外的流程控制,其核心工作原理在于收集测试过程中的打印日志,然后与期望字符串做比较,最后得出是否一致的报告 ...

  8. LaTex Font Size 字体大小

    目录 命令 效果图 命令 LaTex中字体大小由以下命令控制: \tiny \scriptsize \footnotesize \small \normalsize \large \Large \LA ...

  9. 编写shell脚本需要特别关注的注意点

    shell脚本中的条件判断句式 1. if [ condition ];then statement fi 2. If [ condition ];then statement elif [ cond ...

  10. 【bzoj3238】 Ahoi2013—差异

    http://www.lydsy.com/JudgeOnline/problem.php?id=3238 (题目链接) 题意 给出一个字符串,求${\sum_{1<=i<j<=n} ...