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

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 running
    		MongoClient m1 = new MongoClient();
    
    		// use test as a database,use your database here
    		DB db = m1.getDB("test");
    
    		// fetch the collection object ,car is used here,use your own
    		DBCollection coll = db.getCollection("car");
    
    		// builds query for car whose speed is less than 45
    		BasicDBObject b1 = new BasicDBObject("speed", new BasicDBObject("$lt",
    				45));
    
    		// invoke remove method
    		WriteResult c1 = coll.remove(b1);
    
    		// print the number of documents using getN method
    		System.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 fetched
    		DBObject doc = coll.findOne(); // get first document
    
    		// remove the document fetched using findOne method
    		WriteResult 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 object
    		WriteResult 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 operation
    		removeByQuery();
    		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 running
    		MongoClient m1 = new MongoClient();
    
    		// use test as a database,use your database here
    		DB db = m1.getDB("test");
    
    		// fetch the collection object ,car is used here,use your own
    		DBCollection coll = db.getCollection("car");
    
    		// builds query for car whose speed is less than 45
    		BasicDBObject b1 = new BasicDBObject("speed", new BasicDBObject("$lt",
    				45));
    
    		// invoke remove method
    		WriteResult c1 = coll.remove(b1);
    
    		// print the number of documents using getN method
    		System.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 fetched
    		DBObject doc = coll.findOne(); // get first document
    
    		// remove the document fetched using findOne method
    		WriteResult 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 object
    		WriteResult 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 operation
    		removeByQuery();
    		removeSingleDoc();
    		removeAllDocs();
    
    	}
    
    }

    上面程序的输出是:

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值