Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Harsh Saxena

Greenhorn
+ Follow
since Apr 06, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Harsh Saxena

Campbell Ritchie wrote:

Harsh Saxena wrote:. . . . I am actually on the Mongo university course "MongoDb for Java developers" and the package name is as directed in the video namely "com.mongodb.

In which case they are entitled to use the package name themselves. Remember the naming conventions next time

Here's is what I can see when I hover my mouse over getDB(). . .

That is useful information, but you should be able to get the API documentation for the method by hovering your mouse over the method name. Even more useful information if you can find it.

I have tried casting to MongoDb but that has not helped. . . .

No, you cannot cast to incompatible types. You will (I think) have to declare the database as type DB.

I have posted this problem on the mongodb unversity dicussion . . .

That is more useful information for us; please also tell them on that site that you have posted here, to avoid duplication of effort.

I guess as I am running a week behind no one will look at this query.

Don't be so pessimistic about them



Hi,

I could finally resolve this one on my own .  

I update the mongo-java-driver to the latest version of 3.6.3, that gave the option of " mongoClient.getDatabase()" insetad of getDB(); this has sorted out the issue and I am able to insert a record in the database through my Java front end code.

Thanks for your assistance
3 years ago

Campbell Ritchie wrote:Welcome to the Ranch
Thank you for using code tags, but you didn't quite have them right: the [code=java] part goes before the code and the [/code] part after; I corrected it by moving the second part, so doesn't it look better. Also, you appear to have asked the same question twice, so I have deleted the other post.
Don't use com.mongodb as a package name; as you will see from the Java™ Tutorials, that name belongs to somebody else. Try database or something as a package name.

Let's see if we can't find a MongoDB tutorial. Let's try here. It doesn't appear that you have a connectivity problem, but something to do with types which the compiler doesn't like. About ⅓ way down that tutorial, it gives you this line of code:-Compare that with your line 21, which appears to be the location of the error (thank you for supplying a good copy of the error message .) It seems, as the compiler error message says, that you have something which returns a DB reference, so you appear to have declared the database object with the wrong type. Do you know where the Java® API documentation about MongoDB is? That might be very helpful. Does the documentation appear when you hover your mouse over “getDB” on Eclipse?



I am new to CodeRanch; still learning the nuances of code ranch.

Here's some more information that I can give you.  I am actually on the Mongo university course "MongoDb for Java developers" and the package name is as directed in the video namely "com.mongodb.  Here's is what I can see when I hover my mouse over getDB()  (see attachments)

I have tried casting to MongoDb but that has not helped.  Following is the error after casting to Mongo DB

"Connected to the target VM, address: '127.0.0.1:49921', transport: 'socket'
Exception in thread "main" java.lang.ClassCastException: com.mongodb.DBApiLayer cannot be cast to com.mongodb.client.MongoDatabase
at com.mongodb.InsertTest.main(InsertTest.java:21)
Disconnected from the target VM, address: '127.0.0.1:49921', transport: 'socket'"

I have posted this problem on the mongodb unversity dicussion as well but I guess as I am running a week behind no one will look at this query.

3 years ago
Hello!

I am trying to insert a record in Mongo DB using the following code.  The error that I get is "Error:(21, 51) java: incompatible types: com.mongodb.DB cannot be converted to com.mongodb.client.MongoDatabase".  Basically it's this line of code "MongoDatabase database = mongoClient.getDB("course");" that's giving the error.  I just want to insert  a record in Mongodb.  I am using Mongodb version 3.2, Java 8, and Intellj Idea as my IDE.  I am not able to get MongoDatabase database = mongoClient.getDatabase("course") method via intellisensef.

Kindly suggest whats going wrong.
3 years ago