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. TCP源码—epoll源码及测试

    一.epoll_create & epoll_create1 SYSCALL_DEFINE1(epoll_create, int, size) sys_epoll_create->sys ...

  2. Alpha阶段事后诸葛分析

    一.设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要是解决在宿舍中购买商品的软件,不同于淘宝等软件,本软件主要是用于学生开设的店铺及宿 ...

  3. Node初识笔记 1第一周

    #下载安装好node > https://nodejs.org/en/ #  打开cmd  调整好执行路径 . 1.js是JS文件名,cd调招路径,‘node’+空格 +JS文件名(带上扩展名) ...

  4. Redis (二)_ jedis的使用

    Jedis 是 Redis 官方首选的 Java 客户端开发包 虚拟机设置 查看虚拟机的ip ifconfig 将虚拟机的6379端口打开 #运行下面的命令 如果是新建的一个新的 文件,你需要先安装 ...

  5. 深入理解JAVA虚拟机阅读笔记3——垃圾回收器

    一.垃圾收集器总览 新生代:Serial. ParNew. Parallel Scavenge 老年代:CMS.Serial Old. Parallel Old 最新的:G1 并行和并发的区别: 并行 ...

  6. POJ1637_Sightseeing tour

    给一个联通图,有的是单向边,有的是双向边,问是否存在欧拉回路. 乍一看毫无思路,可以这样来搞,对于每条无向边,我们随便指定一个方向,看看是否能够做到所有点的度数之和为偶数. 接下来,对于我们指定的边, ...

  7. CSU1392(NCPC2013)_Number Trick

    给一个小数X,找个A使得:AX=(A循环左移一位) 首先,假设A为一个满足题目条件的数,有n个数位,且最高位数字为A0. 那么可列出方程:AX=(A-A0*10n-1)*10+A0 ————>& ...

  8. Visual Studio Code运行Python文件出现 “Linter pylint is not installed ”提示解决办法

    运行Python代码后出现 “Linter pylint is not installed ”提示 只需要添加一行代码就可以解决 { "python.pythonPath": &q ...

  9. 题解 CF1005A 【Tanya and Stairways】

    楼上别说这个题水,这个题可能还真有不知道的知识点. 看到这个题,想到刚学的单调栈. 单调栈? 单调栈和单调队列差不多,但是我们只用到它的栈顶. 单调,意思就是一直递增或者递减. 这跟这个题有什么关系? ...

  10. CF915E Physical Education Lessons

    题意: Alex高中毕业了,他现在是大学新生.虽然他学习编程,但他还是要上体育课,这对他来说完全是一个意外.快要期末了,但是不幸的Alex的体育学分还是零蛋! Alex可不希望被开除,他想知道到期末还 ...