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. 安装centos6及安装redhat6后的配置

    一.安装centos6 在引导到镜像后,选择: 我选择第二个,使用基本的显卡驱动安装系统 #第一个也是可以选的(安装或升级现有的系统) 之后,与 RHEL5 同样,使用光盘引导安装,系统会提示我们是否 ...

  2. php SPL标准库iterator和ArrayAccess的学习

    最近在补充学习php基础的时候看到了spl的介绍,学习了一下iterator和arrayAccess的使用,iterator主要是对象的迭代,一般可以用在容器里面,或者工厂模式里面,最常见的应用场景就 ...

  3. mysql EXPLAIN 参数表

    测试样式: 参数详情:

  4. QObject 源代码阅读

    我们进入 qt/src 文件夹.你可能对这里的目录名时曾相识,因为几乎这里的所有文件夹名都对应着 Qt 的模块的名字:gui,network,multimedia等等.我们从最核心的 QtCore 开 ...

  5. E-commerce 中促销系统的设计

    在电商平台中,促销是必不可少的营销手段,尤其在国内 各种玩法层出不穷,最开始的满减/秒杀 到优惠卷 再到 拼团/砍价等等 一个良好的促销系统应该具备易于扩展,易于统计促销效果等特点,在遇到秒杀类促销时 ...

  6. 【HLSDK系列】groupinfo的基本用法

    如果你经常写AMXX,你应该会知道有个 pev->groupinfo 变量,但我猜大部分人都不会用这个变量,这个变量涉及很多实体处理功能,下面列举几个最常用的. ① 玩家与非玩家实体之间的碰撞检 ...

  7. 02.基于IDEA+Spring+Maven搭建测试项目--详细过程

    一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息 ...

  8. P3293 [SCOI2016]美味

    题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi .因此,第 ...

  9. P1198 [JSOI2008]最大数

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度.(L>0) ...

  10. Vivian's Problem UVA - 1323(梅林素数+状压二进制)

    借鉴:https://blog.csdn.net/miku23736748/article/details/52135932 https://blog.csdn.net/acm_cxlove/arti ...