error java.lang.NoSuchMethodError in java-mongodb example

13,856

Issue with

MongoClient mongo = new MongoClient("localhost", 27017);

Try with format

MongoClient mongo = new MongoClient(new MongoClientURI("mongodb://127.0.0.1:27017"));
Share:
13,856
Vikas
Author by

Vikas

Updated on December 01, 2022

Comments

  • Vikas
    Vikas over 1 year

    I have been trying to execute my first mongodb example in java. Its showing me the error. I have installed everything properly. Any help would be appreciated.it is throwing an exception Exception in thread "main"

    java.lang.NoSuchMethodError: com.mongodb.ReadPreference.primary()Lcom/mongodb/ReadPreference; at com.mongodb.MongoClientOptions$Builder.(MongoClientOptions.java:731) at com.mongodb.MongoClient.(MongoClient.java:151) at com.mongodb.MongoClient.(MongoClient.java:141) at mongoDBExample.main(mongoDBExample.java:21)

    This is my program code.

       public class mongoDBExample {
        public static void main(String[] args) throws UnknownHostException {
    
        try {
    
        /**** Connect to MongoDB ****/
    
        MongoClient mongo = new MongoClient("localhost", 27017);
    
        /**** Get database ****/
        // if database doesn't exists, MongoDB will create it for you
        DB db = mongo.getDB("testdb");
    
        /**** Get collection / table from 'testdb' ****/
        // if collection doesn't exists, MongoDB will create it for you
        DBCollection table = db.getCollection("user");
    
        /**** Insert ****/
        // create a document to store key and value
    
    
        BasicDBObject document = new BasicDBObject();
        document.put("name", "mkyong");
        document.put("age", "30");
        document.put("createdDate", new Date());
        table.insert(new DBObject[] {document});
    
    
        /**** Find and display ****/
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", "mkyong");
    
        DBCursor cursor = table.find(searchQuery);
    
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    
        /**** Update ****/
        // search document where name="mkyong" and update it with new values
        BasicDBObject query = new BasicDBObject();
        query.put("name", "mkyong");
    
        BasicDBObject newDocument = new BasicDBObject();
        newDocument.put("name", "mkyong-updated");
    
        BasicDBObject updateObj = new BasicDBObject();
        updateObj.put("$set", newDocument);
    
        table.update(query, updateObj);
    
        /**** Find and display ****/
        BasicDBObject searchQuery2
            = new BasicDBObject().append("name", "mkyong-updated");
    
        DBCursor cursor2 = table.find(searchQuery2);
    
        while (cursor2.hasNext()) {
            System.out.println(cursor2.next());
        }
    
        /**** Done ****/
        System.out.println("Done");
    
        } catch (MongoException e) {
        e.printStackTrace();
        }
    
      }
    }
    
    • user1211
      user1211 over 7 years
      What version of mongo jar you are using? Which is your line 21?
    • Vikas
      Vikas over 7 years
      @user3632894 mongo-java-driver-3.2.0
    • Vikas
      Vikas over 7 years
      MongoClient mongo = new MongoClient("localhost", 27017); this is the 21st line.
    • user1211
      user1211 over 7 years
      I have tried running your code. It works fine. Make sure you have these import statements import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; import com.mongodb.MongoException; Also you are using deprecated methods.
    • Jim Garrison
      Jim Garrison over 7 years
      This is almost always a difference in versions between the build and deployment environments. Make sure you have the same jar versions in both places.
    • Vikas
      Vikas over 7 years
      i tried reinstalling the mongodb server and tried running it once again.It worked fine for me as well.I dont what was the issue.Thanks anyways :)
  • Vendetta
    Vendetta almost 4 years
    Please add a little more context to your answer.