MongoDB java code compilation issue

449 views
Skip to first unread message

Ahmad S

unread,
Aug 20, 2018, 10:57:57 AM8/20/18
to mongodb-user
I am following this example for Mongodb 3.x  but I am unable to compile my code

code :

/* Example.java */

import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoDatabase;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;

import java.util.List;
import java.util.Set;

public class Example {

    public static void main(String[] args) throws Exception {

        // connect to the local database server
        // NOTE: older versions of the *.jar file do not have a MongoClient class
        //       check your *.jar file to be sure
        //       use the Mongo class instead
        MongoClient mongoClient = new MongoClient( "localhost", 27017);

        // get handle to "mydb"
        MongoDatabase db = mongoClient.getDatabase("mydb");

        // get a collection object to work with
        DBCollection testCollection = db.getCollection("test");

        // make a document and insert it
        BasicDBObject doc = new BasicDBObject("a", "Java");

        testCollection.insert(doc);

        //  lets get all the documents in the collection and print them out
        DBCursor cursor = testCollection.find();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        } finally {
            cursor.close();
        }

        // See if the last operation had an error
//        System.out.println("Last error : " + db.getLastError());

        // see if any previous operation had an error
//        System.out.println("Previous error : " + db.getPreviousError());

        // release resources
        mongoClient.close();
    } 

}



compilation error:

# javac -cp /usr/local/lib/mongo-java-driver-3.8.0.jar:. Example.java
Example.java:27: error: incompatible types: MongoCollection<Document> cannot be converted to DBCollection
        DBCollection testCollection = db.getCollection("test");
                                                      ^
1 error

 

scott.lh...@10gen.com

unread,
Aug 22, 2018, 1:47:52 AM8/22/18
to mongodb-user
Hi Ahmad,

It looks like you're using both the older api ( DBCollection, DBCursor, MongoClient, MongoException) and the newer api (MongoDatabase and com.mongodb.client.* ) together.
I've modified the code you provided with one way I think you could accomplish this.  Also, I'm using a new driver version (3.7.1) but I've included some commented code that works in the older driver version.

We also have a newer getting started guide.  Hope this helps.


Driver Dependency (pom.xml): 
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.7.1</version>
</dependency>

Example Code:

import com.mongodb.client.*;
import org.bson.Document;
import java.util.Iterator;
//for 3.2-3.6 driver
//import com.mongodb.MongoClient;

public class Example {

public static void main(String[] args) throws Exception {

// connect to the local database server
// NOTE: older versions of the *.jar file do not have a MongoClient class
// check your *.jar file to be sure
// use the Mongo class instead

        //3.7 Client Creation (doesn't work in 3.2)
MongoClient mongoClient = MongoClients.create();

//3.2 Client Creation (works up to 3.6, doesn't work in 3.7+)
//MongoClient mongoClient1 = new MongoClient();

// get handle to "mydb"
MongoDatabase db = mongoClient.getDatabase("mydb");

// get a collection object to work with
        MongoCollection testCollection = db.getCollection("test");

// make a document and insert it
        Document doc = new Document("a", "Java");

testCollection.insertOne(doc);

// lets get all the documents in the collection and print them out
        MongoCursor<Document> cursor = testCollection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
            }
} finally {
cursor.close();
}


// See if the last operation had an error
// System.out.println("Last error : " + db.getLastError());

// see if any previous operation had an error
// System.out.println("Previous error : " + db.getPreviousError());

// release resources
mongoClient.close();
}

}

Cheers,
Scott
Reply all
Reply to author
Forward
0 new messages