在 java 中使用 MongoDB db.collection.remove(query)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20134261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use MongoDB db.collection.remove(query) in java
提问by Kirby
Is there a way in the MongoDB Java driver to call the db.collection.remove(query)
method that I see in the MongoDB shell documentation?
That is, I know the exact criteria that I need to find all the documents I want to delete from MongoDB, but I can't find a way to make one call to remove those records in one trip. All that I can figure out is to find the documents and then delete them one by one.
MongoDB Java 驱动程序中有没有办法调用db.collection.remove(query)
我在 MongoDB shell 文档中看到的方法?也就是说,我知道从 MongoDB 中查找要删除的所有文档所需的确切条件,但是我找不到一种方法可以在一次旅行中通过一次调用来删除这些记录。我唯一能想到的就是找到这些文件,然后将它们一一删除。
I see this http://docs.mongodb.org/manual/reference/method/db.collection.remove/which implies there should be a way to do it, but I can't figure out the Java calls to get me that to that call.
我看到这个 http://docs.mongodb.org/manual/reference/method/db.collection.remove/这意味着应该有办法做到这一点,但我无法弄清楚 Java 调用让我知道到那个电话。
Thank you for your help
感谢您的帮助
采纳答案by joews
To remove documents with an age
property of 25.
删除age
属性为 25 的文档。
MongoClient mongo = new MongoClient(new ServerAddress("localhost", 27017));
DB db = mongo.getDB("thedb");
DBCollection collection = db.getCollection("test");
BasicDBObject query = new BasicDBObject();
query.append("age", 25);
collection.remove(query);
DBCollectionand BasicDBObjectare two of the most important classes in the Java API.
DBCollection和BasicDBObject是 Java API 中两个最重要的类。
回答by Hiren
Also to remove specific values from you document you can use following code with Mongo Java 3.2
此外,要从您的文档中删除特定值,您可以在 Mongo Java 3.2 中使用以下代码
Document docToDelete = new Document("Designation", "SE-1");
objDbCollection.findOneAndUpdate(new Document("Company", "StackOverflow"), new Document("$unset", docToDelete));
Above code will first find document having company = StackOverflow and then unset (remove) Designation = SE-1 key/value from that document.
上面的代码将首先找到具有 company = StackOverflow 的文档,然后从该文档中取消设置(删除)Designation = SE-1 键/值。
回答by MADHUR GUPTA
Add and Update Mongo
添加和更新 Mongo
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();
List<Integer> list = new ArrayList<Integer>();
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();
}