MongoDB remove method removes a single document or all the documents present in the collection or the documents with specific criteria.

MongoDB remove方法将删除单个文档或集合中存在的所有文档或具有特定条件的文档。

The syntax for remove is

删除的语法是

db.collection.remove(<query>,{justOne: <boolean>,writeConcern: <document>}
)

query: specifies the documents to be removed from the collection.

query :指定要从集合中删除的文档。

justOne: boolean parameter with value true that limits the deletion to a single document.

justOne :值为true的布尔参数,将删除范围限制为单个文档。

writeConcern: guarantees the reporting on success of a write operation.

writeConcern :保证报告写操作成功。

This method returns the object that contains the status of write operation indicating the number of documents that have been removed successfully.

此方法返回包含写操作状态的对象,该状态指示已成功删除的文档数。

Consider the following examples for remove method:

考虑以下示例的remove方法:

  1. To remove all documents in a collection: invoke remove method with an empty query document as

    db.car.remove({})

    It produces following output:

    This removes all the documents present in the collection car and provides the output indicating that 6 records were removed.

    删除集合中的所有文档 :调用带有一个空查询文档的remove方法为

    db.car.remove({})

    它产生以下输出:

    这将删除收集车中存在的所有文档,并提供指示删除了6条记录的输出。

  2. To remove the documents that match the criteria entered by the user: call remove method specifying the query parameter.
    >db.car.remove( {speed : {$lt:45}})

    Output:

    This deletes the documents in the car collection having speed less than 45 and notifies in the output that two documents were removed from the collection.

    要删除符合用户输入条件的文档 :调用指定查询参数的remove方法。

    >db.car.remove( {speed : {$lt:45}})

    输出:

    这将删除速度小于45的汽车收藏集中的文档,并在输出中通知已从收藏集中删除了两个文档。

  3. To remove a single document that match the criteria entered: invoke remove method with query criteria and set justOne parameter to 1 or true.
    db.car.remove( { speed: {$gt:51}},1)

    Output:

    This removes the first document in the car collection whose speed is greater than 51 and indicates that 1 record is removed from the collection.

    要删除符合输入条件的单个文档 :用查询条件调用remove方法,并将justOne参数设置为1或true。

    db.car.remove( { speed: {$gt:51}},1)

    输出:

    这会删除汽车集合中速度大于51的第一个文档,并指示从集合中删除了1条记录。

  4. The $isolated :1 isolates the query as specified in the parameter. By using the $isolated option, we can ensure that no client sees the changes until the operation completes or errors out.
    db.car.remove( { speed: { $gt:30 }, $isolated:1 })

    Output:

    This removes the documents in the car collection whose speed is greater than 20 and since isolation is set to 1 the operation is atomic which means none of the users will be able to see this change until the whole set of operation is completed.

    MongoDB删除Java程序 (MongoDB Remove Java Program)

    In this section let’s write a java program to delete the documents from the collection.

    package com.journaldev.mongodb;import java.net.UnknownHostException;import com.mongodb.BasicDBObject;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.DBObject;
    import com.mongodb.MongoClient;
    import com.mongodb.WriteResult;public class MongoDBRemove {public static void removeByQuery() throws UnknownHostException {// Get a new connection to the db assuming that it is runningMongoClient m1 = new MongoClient();// use test as a database,use your database hereDB db = m1.getDB("test");// fetch the collection object ,car is used here,use your ownDBCollection coll = db.getCollection("car");// builds query for car whose speed is less than 45BasicDBObject b1 = new BasicDBObject("speed", new BasicDBObject("$lt",45));// invoke remove methodWriteResult c1 = coll.remove(b1);// print the number of documents using getN methodSystem.out.println("Number of documents removed:" + c1.getN());}public static void removeSingleDoc() throws UnknownHostException {MongoClient m1 = new MongoClient();DB db = m1.getDB("test");DBCollection coll = db.getCollection("car");BasicDBObject b1 = new BasicDBObject("speed", new BasicDBObject("$gt",45));// invoke findOne so that the first document is fetchedDBObject doc = coll.findOne(); // get first document// remove the document fetched using findOne methodWriteResult r1 = coll.remove(doc);System.out.println("------------------------------------");System.out.println("Number of documents removed:" + r1.getN());}public static void removeAllDocs() throws UnknownHostException {MongoClient m1 = new MongoClient();DB db = m1.getDB("test");DBCollection coll = db.getCollection("car");// remove all documents in the collection with empty objectWriteResult r1 = coll.remove(new BasicDBObject());System.out.println("------------------------------------");System.out.println("Number of documents removed:" + r1.getN());}public static void main(String[] args) throws UnknownHostException {// invoke all the methods to perform remove operationremoveByQuery();removeSingleDoc();removeAllDocs();}}

    Output of the above program is:

    That’s all for removing documents from a collection in MongoDB, we will look into more MongoDB features in coming posts.

    db.car.remove( { speed: { $gt:30 }, $isolated:1 })

    输出:

    这会删除汽车集合中速度大于20的文档,并且由于将隔离设置为1,因此该操作是原子操作,这意味着在完成整个操作之前,所有用户都无法看到此更改。

    在本节中,我们编写一个Java程序以从集合中删除文档。

    package com.journaldev.mongodb;import java.net.UnknownHostException;import com.mongodb.BasicDBObject;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.DBObject;
    import com.mongodb.MongoClient;
    import com.mongodb.WriteResult;public class MongoDBRemove {public static void removeByQuery() throws UnknownHostException {// Get a new connection to the db assuming that it is runningMongoClient m1 = new MongoClient();// use test as a database,use your database hereDB db = m1.getDB("test");// fetch the collection object ,car is used here,use your ownDBCollection coll = db.getCollection("car");// builds query for car whose speed is less than 45BasicDBObject b1 = new BasicDBObject("speed", new BasicDBObject("$lt",45));// invoke remove methodWriteResult c1 = coll.remove(b1);// print the number of documents using getN methodSystem.out.println("Number of documents removed:" + c1.getN());}public static void removeSingleDoc() throws UnknownHostException {MongoClient m1 = new MongoClient();DB db = m1.getDB("test");DBCollection coll = db.getCollection("car");BasicDBObject b1 = new BasicDBObject("speed", new BasicDBObject("$gt",45));// invoke findOne so that the first document is fetchedDBObject doc = coll.findOne(); // get first document// remove the document fetched using findOne methodWriteResult r1 = coll.remove(doc);System.out.println("------------------------------------");System.out.println("Number of documents removed:" + r1.getN());}public static void removeAllDocs() throws UnknownHostException {MongoClient m1 = new MongoClient();DB db = m1.getDB("test");DBCollection coll = db.getCollection("car");// remove all documents in the collection with empty objectWriteResult r1 = coll.remove(new BasicDBObject());System.out.println("------------------------------------");System.out.println("Number of documents removed:" + r1.getN());}public static void main(String[] args) throws UnknownHostException {// invoke all the methods to perform remove operationremoveByQuery();removeSingleDoc();removeAllDocs();}}

    上面程序的输出是:

    这就是从MongoDB中的集合中删除文档的全部内容,我们将在以后的文章中探讨更多MongoDB功能。

翻译自: https://www.journaldev.com/6326/mongodb-remove-example-using-mongo-shell-and-java-driver

使用Mongo Shell和Java驱动程序删除MongoDB的示例相关推荐

  1. 使用Mongo Shell和Java驱动程序的MongoDB Map Reduce示例

    Map Reduce is a data processing technique that condenses large volumes of data into aggregated resul ...

  2. 使用Shell和Java驱动程序的MongoDB身份验证配置示例

    Authentication enables user to verify identity before connecting to the database. At first, a user w ...

  3. MongoDB存在使用Mongo Shell和Java驱动程序的示例

    This checks the document for the existence of the fields in the specified collection. 这将检查文档中指定集合中是否 ...

  4. 如何从命令行删除MongoDB数据库?

    从我的bash提示中最简单的方法是什么? #1楼 您还可以使用" heredoc": mongo localhost/db <<EOF db.dropDatabase( ...

  5. Docker安装MoogoDB, 进入容器, mongo shell操作mongoDB

    安装MoogoDB, 进入容器, mongo shell操作mongoDB [ 包含 Docker-Compose方式.普通方式 ] 文章目录 安装MoogoDB, 进入容器, mongo shell ...

  6. MongoDB学习(五)使用Java驱动程序3.3操作MongoDB快速入门

    [引言] 毕竟现在MongoDB还是出于成长阶段,所以现在网上相关的资料很少,而且大部分还都是针对于MongoDB的老版本的.再加上MongoDB的频繁升级.重大更新等等,导致菜鸟学习的难度增大. 好 ...

  7. mongo shell连接到mongoDB及shell提示符下执行js脚本

    同mysql数据库类似,mongoDB也可通过mongo客户端连接到mongod服务器来进行绝大多数日常管理.这个命令行工具就是mongo,在mysql中则是mysql.通过mongo命令可以连接到本 ...

  8. java mongodb 关闭连接_如何在mongodb上使用java驱动程序保持连接池关闭?

    我正在从 java驱动程序2.12.3升级到3.3.0.奇怪的是,收集池似乎突然"起作用". 我的设置如下: Connection在主线程中建立: mongoClient = ne ...

  9. MongoDB学习笔记(3)- Mongo Shell 常用查询命令

    MongoDB学习笔记(3)- Mongo Shell 常用查询命令 本文所使用的MongoDB版本为 4.0.10 > db.version(); 4.0.10 一.find 命令进行简查询 ...

最新文章

  1. Java线程组(ThreadGroup)使用
  2. java如何理解继承性_理解 Java 的三大特性之继承
  3. jquery.easing.js(转)
  4. EventBus使用实例,观察者模式
  5. redis java连接出错_redis连接错误与spring boot
  6. 浅析RTB和RTA(二)
  7. Python注释的写作笔记
  8. GPS坐标单位(度分秒)的换算方法
  9. ChartCube - 图表魔方:阿里出品的免费在线图表制作工具,简单好用还漂亮
  10. java tbase_TBase备份恢复实验
  11. win7右键菜单管理_Windows右键菜单下载 Windows右键菜单管理软件 v1.0 绿色免费版 下载...
  12. 怎么用计算机平方,手机计算器平方怎么按
  13. STC89C51系列 EEPROM测试程序 证明扇区512个字节的擦除
  14. 一组数据,带你读懂“2021中国民营企业500强”背后深意
  15. 彻底理解numpy中的axis
  16. 浅谈Java中类的相关内容
  17. 无人机在抢险救灾的优化运用---2017年中国研究生数学建模竞赛A题 (试题+优秀论文)
  18. dpdk 问题分析:ice 100G 网卡 rx_packets 与 rx_bytes 统计问题
  19. JAVA使用springboot整合佳博标签打印机(一)
  20. 意大利vs澳大利亚 1:0

热门文章

  1. JQuery操作cookie插件
  2. [转载] python中set使用介绍
  3. 去除input的自动填充色
  4. JS编写自己的富文本编辑器
  5. MySql 获取当前节点及递归所有上级节点
  6. EBS R12.2 创建应用层的启动和关闭脚本
  7. EnterpriseLibrary 介绍
  8. 剑指offer-21.栈的压入弹出序列
  9. Git基础教程(四)
  10. dapperpoco mysql_DapperPoco -- 基于Dapper的、轻量级的、高性能的、简单的、灵活的ORM框架...