【发布时间】:2016-12-07 04:48:27
【问题描述】:
我有一个 MongoDAO 类,它具有以下基本 Mongo CRUD 操作的代码。我正在使用 collection.updateOne 方法的代码行未编译并引发错误“MongoCollection 类型中的方法 updateOne(Bson, Bson) 不适用于参数(文档)” .我需要传递一个 ToolThing 类型的对象并使用该对象来更新 mongodb 上的现有文档。如何解决这个问题而不必参考对象 ToolThing 的各个参数?
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();
}
}
【问题讨论】: