MongoDB - MongoDB CRUD Operations, Delete Documents
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:
- A 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>, ... } - A 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:
db.collection.findOneAndDelete().
findOneAndDelete() provides a sort option. The option allows for the deletion of the first document sorted by the specified order.
db.collection.findOneAndModify().
db.collection.findOneAndModify() provides a sort option. The option allows for the deletion of the first document sorted by the specified order.
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的更多相关文章
- MongoDB - MongoDB CRUD Operations, Insert Documents
MongoDB provides the following methods for inserting documents into a collection: db.collection.inse ...
- MongoDB - MongoDB CRUD Operations, Update Documents
Update Methods MongoDB provides the following methods for updating documents in a collection: Method ...
- 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 ...
- 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 ...
- MongoDB - MongoDB CRUD Operations, Query Documents
Query Method MongoDB provides the db.collection.find() method to read documents from a collection. T ...
- 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 ...
- MongoDB - MongoDB CRUD Operations
CRUD operations create, read, update, and delete documents. Create Operations Create or insert opera ...
- MongoDB - MongoDB CRUD Operations, Bulk Write Operations
Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...
- Mongodb系列- CRUD操作介绍
---恢复内容开始--- 一 Create 操作 在MongoDB中,插入操作的目标是一个集合. MongoDB中的所有写入操作在单个文档的层次上都是原子的. For examples, see In ...
随机推荐
- 今年暑假要AC
今年暑假要AC 在这个大学的第一个的暑假,谁不想回去high呢~ 但是,这是不行的,还没有AC,你能回去吗?高三那年的暑假怎么玩的,现在补回来吧...有规模有计划有氛围的学习就是:优点多效率好激情足~ ...
- WPF里面制作圆角文本框
转自:http://www.cnblogs.com/mengxin523/archive/2010/04/04/1704448.html 本以为WPF里面的XAML会很强大,可以设置很多属性,比如文本 ...
- [知乎]老狼:UFS VS NVMe
https://zhuanlan.zhihu.com/p/26652622 最近某手机厂商的闪存门在知乎上被人踢爆,在所谓“爵士水军”和“友商水军”的口水大战中,至少eMMC, UFS等火星名词被广泛 ...
- 【Java并发编程】之十二:线程间通信中notifyAll造成的早期通知问题
如果线程在等待时接到通知,但线程等待的条件还不满足,此时,线程接到的就是早期通知,如果条件满足的时间很短,但很快又改变了,而变得不再满足,这时也将发生早期通知.这种现象听起来很奇怪,下面通过一个示例程 ...
- A New Function LightOJ - 1098()
题意 求 1 - n的的所有数的因子(不包括自身和1)和 对于一个数 i ,以i为因子的数的个数为 n/i 因为不能包括自身 所以 减一 即 n/i-1 这样遍历每一个数 累加即可 但复杂度较 ...
- 【python】vscode python环境配置
安装python插件:ext install python 配置flake8:pip install flake8 配置yapf:pip install yapf(在VScode中按Alt+Shift ...
- Mininet 系列实验(二)
实验内容 分别通过命令行创建.Python脚本编写以及交互式界面创建来熟悉Mininet的基本功能. 参考 Mininet命令延伸实验扩展 实验环境 虚拟机:Oracle VM VirtualBox ...
- 洛谷 P5105 不强制在线的动态快速排序
P5105 不强制在线的动态快速排序 题目背景 曦月最近学会了快速排序,但是她很快地想到了,如果要动态地排序,那要怎么办呢? 题目描述 为了研究这个问题,曦月提出了一个十分简单的问题 曦月希望维护一个 ...
- 中行P1签名及验签
分享中国银行快捷.NET P1签名和验签方法代码中ReturnValue为自定义类型请无视 #region 验证签名 /// <summary> /// 验证签名 /// </sum ...
- 【bzoj3122】 Sdoi2013—随机数生成器
http://www.lydsy.com/JudgeOnline/problem.php?id=3122 (题目链接) 题意 对于一个数列${X_i}$,其递推式为:${X_{i+1}=(a*X_i+ ...