ZCode
ZCode

Reputation: 710

Java Driver MongoDB updateone

I have a class MongoDAO which has the below code for basic Mongo CRUD operations. The line in the code where I am using collection.updateOne method is not compiling and throwing the error "The method updateOne(Bson, Bson) in the type MongoCollection is not applicable for the arguments (Document)". I need to pass an object of type ToolThing and use the object to update an existing document on mongodb. How do I resolve this without having to refer to individual parameters of the object 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();
    }

}

Upvotes: 2

Views: 19185

Answers (1)

Clement Amarnath
Clement Amarnath

Reputation: 5476

Yes you will be getting that exception, Since MongoCollection.updateOne should have two parameters, first parameter is the condition to find the document which need to be updated and the second parameter is the actual update.

Refer the examples given in the below posts.

https://docs.mongodb.com/getting-started/java/update/

MongoDB update using Java 3 driver

Upvotes: 3

Related Questions