当我尝试通过java从mongodb中的集合中删除文档时,我的代码有错误

时间:2017-08-23 14:13:14

标签: java mongodb jsp

我可以通过点击上一个jsp文件中的删除按钮

获得真正的ID
try{

    MongoClient client = new MongoClient("localhost",27017);
    DB db = client.getDB("crawler");
    DBCollection collection=db.getCollection("enduser");

    DBObject doc=new BasicDBObject();

    DBCursor curs = collection.find();

    String value = request.getParameter("_id");     

    collection.remove((DBObject)doc.put("_id",value));

    System.out.println(value+"");       

}catch(UnknownHostException e){

    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:1)

value必须是ObjectId,而不是String。试试这个:

collection.remove((DBObject)doc.put("_id", new ObjectId(value)));