原文出自:http://www.mkyong.com/mongodb/java-mongodb-delete-document/
返回目录:http://ysj5125094.iteye.com/blog/2192754
Java MongoDB : Delete document
In this tutorial, we show you how to use collection.remove()
to delete documents from the collection.
译:在本教程中,我们将向你展示使用 collection.remove() 从集合中删除文档。
1. Test Data
Insert 10 documents from number 1 to 10 for testing.
译:插入number 为 1 到 10的 10条文档。
for (int i=1; i <= 10; i++) { collection.insert(new BasicDBObject().append("number", i)); }
2. DBCollection.remove()
See below code snippets to delete documents.(译:看下面删除文档的代码片断)
Example 1
Get first document and delete it. In this case, number = 1 is deleted.
译:获取第一个文档并删除他,相当于删除number = 1的文档。
DBObject doc = collection.findOne(); //get first document collection.remove(doc);
Example 2
Puts query in a BasicDBObject
. In this case, number = 2 is deleted.
译:向BasicDBObject对象中put一个number = 2 ,再删除。
BasicDBObject document = new BasicDBObject(); document.put("number", 2); collection.remove(document);
And Operator?(译:and 操作符)
Two common mistakes :(译:两个常见的错误)
1. A query like this only delete number = 3.
译:只删除number=3的。
BasicDBObject document = new BasicDBObject(); document.put("number", 2); document.put("number", 3); //override above value 2,会重写上面的值 collection.remove(document);
2. Nice try below, but query like this will not work, it will delete NOTHING.
译:这是种不错的尝试,但是这种尝试,将不会删除任何数据。
BasicDBObject document = new BasicDBObject(); List<Integer> list = new ArrayList<Integer>(); list.add(7); list.add(8); document.put("number", list); collection.remove(document);
For “AND” query, you need to use “$in” or “$and” operator, see example 5.
译:使用"AND"查询,你需要使用"$in" 或 "$and"操作符,see example 5.
Example 3
Use BasicDBObject
directly. In this case, number = 3 is deleted.
译:直接使用BasicDBObject
对象。在这种情况下,number=3是可以删除的。
collection.remove(new BasicDBObject().append("number", 3));
Example 4
Puts a $gt
operator in a BasicDBObject
object. In this case, number = 10 is deleted.
译:将 $gt 操作符put BasicDBObject对象。在这种情况下,number=10是可以删除的。
BasicDBObject query = new BasicDBObject(); query.put("number", new BasicDBObject("$gt", 9)); collection.remove(query);
Example 5
Puts a $in
operator in a BasicDBObject
object, constructs the query in ArrayList. In this case, number = 4 and number = 5 are deleted.
译:put一个 $in
操作符到 BasicDBObject
对象,构建查询列表。在这种情况下,number=4和number=5被删除。
BasicDBObject query2 = new BasicDBObject(); Listlist = new ArrayList (); list.add(4); list.add(5); query2.put("number", new BasicDBObject("$in", list)); collection.remove(query2);
More MongoDB Operators
For more operators, read this MongoDB operators quick reference.
译:更多操作符,请阅读此文档(MongoDB operators quick reference.)。
Example 6
Use cursor to delete all available documents. (Not recommended, prefer example 7)
译:使用游标,删除所有可用文档(不推荐使用,建议选择example 7)
DBCursor cursor = collection.find(); while (cursor.hasNext()) { collection.remove(cursor.next()); }
Example 7
Pass an empty BasicDBObject, and the entire documents will be deleted.
译:传递一个空的 BasicDBObject 对象,整个文档会被删除。
collection.remove(new BasicDBObject());
Example 8
It deletes the entire documents and drop the collection.
译:删除所有文档及整个集合。
collection.drop();
Example 9
The remove()
will returns a WrireResult
object, it contains useful information about the remove operation. And you can uses getN()
to get the number of documents affected.
译:使用 remove()
命令将返回一个 WrireResult
对象,他包含删除操作的信息,你可以使用 getN()
方法得到影响文档的行数。
WriteResult result = collection.remove(query2); System.out.println("Number of documents are deleted : " + result.getN());
3. Full Example
Full example to show the different ways to delete documents.
package com.mkyong.core; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * Java MongoDB : Delete document * @author mkyong */ public class App { public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("yourdb"); // get a single collection DBCollection collection = db.getCollection("dummyColl"); //insert number 1 to 10 for testing for (int i=1; i <= 10; i++) { collection.insert(new BasicDBObject().append("number", i)); } //remove number = 1 DBObject doc = collection.findOne(); //get first document collection.remove(doc); //remove number = 2 BasicDBObject document = new BasicDBObject(); document.put("number", 2); collection.remove(document); //remove number = 3 collection.remove(new BasicDBObject().append("number", 3)); //remove number > 9 , means delete number = 10 BasicDBObject query = new BasicDBObject(); query.put("number", new BasicDBObject("$gt", 9)); collection.remove(query); //remove number = 4 and 5 BasicDBObject query2 = new BasicDBObject(); Listlist = new ArrayList (); list.add(4); list.add(5); query2.put("number", new BasicDBObject("$in", list)); collection.remove(query2); //print out the document DBCursor cursor = collection.find(); while(cursor.hasNext()) { System.out.println(cursor.next()); } collection.drop(); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } }
Output…
{ "_id" : { "$oid" : "4dc7a6989e3a66c5faeee757"} , "number" : 6} { "_id" : { "$oid" : "4dc7a6989e3a66c5faeee758"} , "number" : 7} { "_id" : { "$oid" : "4dc7a6989e3a66c5faeee759"} , "number" : 8} { "_id" : { "$oid" : "4dc7a6989e3a66c5faeee75a"} , "number" : 9} Done
References
- MongoDB operators quick reference
- MongoDB WriteResult JavaDoc