Unable to connect to Mongodb using Java

236 views
Skip to first unread message

Harsh Saxena

unread,
Apr 6, 2018, 10:47:24 PM4/6/18
to mongodb-user
Hello!


I am trying to create db, collection and then insert a record in it using Java frontend. Following is the code, the error the I am getting is as below 

"Error:(21, 51) java: incompatible types: com.mongodb.DB cannot be converted to com.mongodb.client.MongoDatabase".  I am unable to insert a record in mongodb.  I am using Mongo DB 3.2 and Java 8, Intellj Idea as my IDE.

Kindly let me know what is going wrong here.

package com.mongodb;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.net.UnknownHostException;

public class InsertTest {
public static void main(String[] args) {


MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");

MongoClient mongoClient = null;
try {
mongoClient = new MongoClient(connectionString);
} catch (UnknownHostException e) {
e.printStackTrace();
}

MongoDatabase database = mongoClient.getDB("course");
MongoCollection<Document> collection = database.getCollection("writers");

Document doc = new Document("name", "Lamu")
.append("age", 30)
.append("profession", "Scientist");

collection.insertOne(doc);



}



}

Wan Bachtiar

unread,
Apr 8, 2018, 8:29:49 PM4/8/18
to mongodb-user

“Error:(21, 51) java: incompatible types: com.mongodb.DB cannot be converted to com.mongodb.client.MongoDatabase”.

Hi Harsh,

The error is caused the use of the legacy DB from mongoClient.getDB("course").
Instead, please substitute with mongoClient.getDatabase("course"). See also MongoClient class description.

You may also find MongoDB Java Driver - Quick Start tutorial useful.

If you have further question, please provide which MongoDB Java Driver version you're using. 

Regards,
Wan.

Harsh Saxena

unread,
Apr 11, 2018, 3:18:48 AM4/11/18
to mongod...@googlegroups.com
Hi Wan,

Thank you for your response.  It did solve the issue, as I was using a legacy driver version I couldn't get the getDatabase() method in intellisense.  I did upgrade the driver to the latest version of 3.6.3 I reckon and that gave me the getDatabase() method to me and it worked.


Regards
Harsh R. Saxena


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see:
​​
https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/b279c839-89a6-4279-aa76-b6902fc2ebc9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages