1 插入例子

db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
]);

2 删除所有

db.inventory.deleteMany({})

3 删除匹配的行

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

4 删除最多一行记录

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

5 注意

1.删除对应元素并不会删除对应的索引
2.删除具有原子性

This page provides examples of delete operations using the following methods in the mongo shell:

The examples on this page use the inventory collection. To populate the inventory collection, run the following:

Copy
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
]);

You can run the operation in the web shell below:

Delete All Documents

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

The following example deletes all documents from the inventory collection:

Copy
db.inventory.deleteMany({})

The method returns a document with the status of the operation. For more information and examples, seedeleteMany().

Delete All Documents that Match a Condition

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

To specify equality conditions, use <field>:<value> expressions in the query filter document:

{ <field1>: <value1>, ... }

query filter document can use the query operators to specify conditions in the following form:

{ <field1>: { <operator1>: <value1> }, ... }

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

The following example removes all documents from the inventory collection where the status field equals"A":

Copy
db.inventory.deleteMany({ status : "A" })

The method returns a document with the status of the operation. For more information and examples, seedeleteMany().

Remove Only One Document that Matches a Condition

To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the db.collection.deleteOne() method.

The following example deletes the first document where status is "D".

Copy
db.inventory.deleteOne( { status: "D" } )

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.

Write Acknowledgement

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

19.Delete Documents-官方文档摘录的更多相关文章

  1. Cocos Creator 加载和切换场景(官方文档摘录)

    Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...

  2. ng的概念层次(官方文档摘录)

    官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...

  3. Cocos Creator 使用计时器(官方文档摘录)

    在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...

  4. angular 模板语法(官方文档摘录)

    https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...

  5. Cocos Creator 生命周期回调(官方文档摘录)

    Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...

  6. Qt元类型(MetaType)注册入门(附一些官方文档的关键摘录)

    昨天调试项目时,突然发现如下消息: QObject::connect: Cannot queue arguments of type 'ERROR_LEVEL' (Make sure 'ERROR_L ...

  7. 20191127 Spring Boot官方文档学习(9.1-9.3)

    9."使用方法"指南 9.1.Spring Boot应用程序 9.1.1.创建自己的FailureAnalyzer FailureAnalyzer被包装在FailureAnalys ...

  8. Spring Data Commons 官方文档学习

    Spring Data Commons 官方文档学习   -by LarryZeal Version 1.12.6.Release, 2017-07-27 为知笔记版本在这里,带格式. Table o ...

  9. hbase官方文档(转)

    FROM:http://www.just4e.com/hbase.html Apache HBase™ 参考指南  HBase 官方文档中文版 Copyright © 2012 Apache Soft ...

  10. HBase 官方文档

    HBase 官方文档 Copyright © 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision ...

随机推荐

  1. Windows Phone 解析手机型号DeviceStatus.DeviceName

    问题的产生 在WP开发过程中难免遇到统计等相关的数据回收,那么当使用DeviceStatus.DeviceName这个来获取设备的名称时经常会得到类似下面的设备信息:     可以看出这样的数据很不直 ...

  2. 【Hadoop】HA 场景下访问 HDFS JAVA API Client

    客户端需要指定ns名称,节点配置,ConfiguredFailoverProxyProvider等信息. 代码示例: package cn.itacst.hadoop.hdfs; import jav ...

  3. [初学WPF]控件大小自适应

    想在Win上自己写点小工具用,GUI自然是免不了的,于是决定学一学WPF,直接拖控件是很方便啊.控件拖出来以后发现运行时改变窗口大小控件不会重绘,搜索了一下发现了解决办法:使用Viewbox控件. V ...

  4. kernel 4.4.12 移植 HUAWEI MU609 Mini PCIe Module

    首先请参考 http://www.cnblogs.com/chenfulin5/p/6951290.html 上一章刚讲了 kernel 3.2.0 移植 MU609 这一章记录新版kernel 的移 ...

  5. #!/bin/sh与#!/bin/bash的区别

    Linux 中的 shell 有很多类型,其中最常用的几种是: Bourne shell (sh).C shell (csh) 和 Korn shell (ksh), 各有优缺点.Bourne she ...

  6. 实战c++中的vector系列--知道emplace_back为何优于push_back吗?

    上一篇博客说道vector中放入struct.我们先构造一个struct对象.再push_back. 那段代码中,之所以不能使用emplace_back,就是由于我们定义的struct没有显示的构造函 ...

  7. 微信小程序6 - 页面之间传参及通知系统封装

    1. 简单传参 wx.navigateTo({ url: '/pages/demo/index/index?id=1' }) /pages/demo/index/index.js 中 onLoad(o ...

  8. CMake Error at cmake/boost.cmake:76 (MESSAGE):

    编译mysql5.7.9的时候报错 CMake Error at cmake/boost.cmake:76 (MESSAGE): You can download it with -DDOWNLOAD ...

  9. 通过http输出流的方式从将html写入到Excel

    private void HtmlResponseToExecl() { //将数据(html)导入到Excel中 Response.Charset = "gb2312"; Res ...

  10. Javascript特效代码大全(420个)(转)

    转载自:Javascript特效代码大全(420个) 收集资料,以便使用+面试+学习  ├ Cookie脚本 ├ 随访问次数变提示 ├ 集成Cookies ├ 使窗口仅弹出一次 ├ 签名提示程序 ├ ...