0

I am trying to connect my java code with mongodb locally hosted. Using dependencies - jUnit v3.8.1 and mongodb-driver-sync v3.11.0. But getting a error while establishing the connection using method "MongoClient" undefined method.

Tried creating a new method for "MongoClient" but there is nothing to create over there.

public static void main(String[] args) {
    MongoClient mongoClient = MongoClient("127.0.0.1", 27017);
    MongoDatabase database = mongoClient.getDatabase("test");
}

The expection is to setup the connection locally between the java code and MongoDB on ("127.0.0.1", 27017) and to perform CRUD method.

2 Answers 2

1

Try using latest sync versions like 4.2.3 , Check if the sync driver version you are using is compatible with the Mongodb version of your cluster/locally installed.

With sync driver 4.2.3 you can connect like this

ConnectionString connString = new ConnectionString(
                            "mongodb+srv://<username>:<password>@<cluster_address>/<database_name>?retryWrites=true&w=majority"
                    );
                    MongoClientSettings settings = MongoClientSettings.builder()
                            .applyConnectionString(connString)
                            .retryWrites(true)
                            .build();
                    MongoClient mongoClient = MongoClients.create(settings);

Also make sure that you have added the dependency for this java driver in your build.gradle (if using gradle) or pom.xml (if using Maven)

These are some more links that will help you

https://docs.mongodb.com/drivers/java/#connect-to-mongodb-atlas

https://mongodb.github.io/mongo-java-driver/

Sign up to request clarification or add additional context in comments.

Comments

1

Try building your connection builder like below:

MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
                     builder.threadsAllowedToBlockForConnectionMultiplier(500);
                     builder.connectionsPerHost(500);
                     builder.connectTimeout(4000);
                     builder.maxWaitTime(4000);
                     builder.socketTimeout(4000);
                     builder.writeConcern(WriteConcern.W1);
                     builder.heartbeatConnectTimeout(4000);
                     builder.maxConnectionIdleTime(4000);
                     MongoClientOptions opts = builder.build();
                     if (db == null) {
                           if (strDBuserName != null && !strDBuserName.trim().equals("")) {
                                  credential = MongoCredential.createCredential(strDatabaseName, strDBuserName, strDBpasswd.toCharArray());
                                  mongo = new MongoClient(new ServerAddress(strDBServerAdd, new Integer(strDBServerPort).intValue()), credential, opts);
                           } else {
                                  mongo = new MongoClient(new ServerAddress(strDBServerAdd, new Integer(strDBServerPort).intValue()), opts);
                           }
                           db = mongo.getDatabase(strDatabaseName);
                     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.