2016-08-02 147 views
0

我有一個MongoDAO類,它具有下面的基本Mongo CRUD操作代碼。在我使用collection.updateOne方法的代碼中的行不是編譯並拋出錯誤「MongoCollection類型中的updateOne(Bson,Bson)方法不適用於參數(文檔)」。我需要傳遞一個ToolThing類型的對象,並使用該對象更新mongodb上的現有文檔。如何解決這個問題,而不必參考對象ToolThing的各個參數?Java驅動程序MongoDB updateone

private String mongoDB; 
private String mongoCollection; 
private List<ToolThing> tools; 
private ToolThing tool; 

MongoClient mongoClient = new MongoClient(); 
MongoDatabase db = mongoClient.getDatabase("test"); 
MongoCollection collection = db.getCollection("tools"); 

public void updateOne(ToolThing input){ 
    try { 
     JSONObject jsonObject = new JSONObject(input); 
     String inputJson = jsonObject.toString(); 
     Document inpDoc = Document.parse(inputJson); 
     collection.updateOne(new Document(inpDoc)); 
    } catch (Exception e) { 
     System.out.println("Mongo Deletion operation failed"); 
     e.printStackTrace(); 
    } 

} 

回答