/* 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();
}
}
# 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
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.7.1</version>
</dependency>
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();
}
}